Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3724810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:46:34+00:00 2026-05-19T06:46:34+00:00

I’m a jQuery newbie, but long-time Perl developer and know regexes well, so I

  • 0

I’m a jQuery newbie, but long-time Perl developer and know regexes well, so I don’t want to use the jQuery Validation plugin.

I’m trying to validate the following 3 web forms, each of which has a text or textarea input field #blah_txt and a submit button #blah_btn:

alt text

I’m validating them with the following jQuery code, which seems to work well:

<script type="text/javascript" src="/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function() {
        $('#mks_btn').click(function(e) {
                $('#cl_txt').css('border', '2px solid black');
                $('#comment_txt').css('border', '2px solid black');

                if (! $('#mks_txt').val().match(/^(CL\s*)?[0-9]{6,}$/i)) {
                        $('#mks_txt').css('border', '2px solid red');
                        e.preventDefault();
                }
        });
        $('#cl_btn').click(function(e) {
                $('#mks_txt').css('border', '2px solid black');
                $('#comment_txt').css('border', '2px solid black');

                if (! $('#cl_txt').val().match(/^(MKS\s*)?[0-9]{6,}$/i)) {
                        $('#cl_txt').css('border', '2px solid red');
                        e.preventDefault();
                }
        });
        $('#comment_btn').click(function(e) {
                $('#mks_txt').css('border', '2px solid black');
                $('#cl_txt').css('border', '2px solid black');

                if ($('#comment_txt').val().length < 2) {
                        $('#comment_txt').css('border', '2px solid red');
                        e.preventDefault();
                }
        });
});
</script>

My (cosmetic) problem is:

When a user enters invalid value into the first text field (“MKS”) and clicks the “Add MKS” button, my code will prevent form submission and make the border of the text field solid red. Then the user changes her mind and decides to enter text into another web form and enters invalid data there again. Then I would have 2 red borders already, which would irritate the user.

I’m trying to workaround this problem by setting the other text fields to solid black on a button click – as you can see above. But this doesn’t look good, because the original border has been something else before the user started entering anything.

So I wonder, if I could restore that original css(‘border’) value for all text input fields somehow – when a button is clicked (or maybe on blur or some other event, indicating that the user has switched to another web form?)

Thank you for any suggestions! Alex

UPDATE

My browser is Firefox 3.6.12 / WinXP, but I want all browsers work of course 🙂

UPDATE 2

The HTML code for the 3 web forms is below and I only have text and links besides that, no further elements at the web page:

<form>
<tr valign="top">
<th>MKS</th><td>
<input name="show_id" type="hidden" value="20110111172527685">
<input name="toggle_mks" id="mks_txt" type="text" size="10" maxsize="10">
<input type="submit" id="mks_btn" value="Add MKS" class="toggle">
</td></tr>
</form>

<form>
<tr bgcolor="#EEEEEE" valign="top">
<th>CL</th><td>
<input name="show_id" type="hidden" value="20110111172527685">
<input name="toggle_cl" id="cl_txt" type="text" size="10" maxsize="10">
<input type="submit" id="cl_btn" value="Add CL" class="toggle">
</td></tr>
</form>

<form>
<tr valign="top">
<th>Comments</th><td>
<input name="show_id" type="hidden" value="20110111172527685">
<textarea name="comment" id="comment_txt" rows="4" cols="60" maxsize="320">
</textarea>
<input type="submit" id="comment_btn" value="Add comment" class="toggle">
</td></tr>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T06:46:35+00:00Added an answer on May 19, 2026 at 6:46 am

    instead of

    $('#mks_txt').css(
    

    create two css-classes in the stylesheet, one for normal state and one for error state. apply the normal state class in the HTML and set the error state class via jQuery.

    .className {
       border: 2px solid red;
    }
    

    use

     $('#mks_txt').addClass("className");
    

    to add a class and use

     $('#mks_txt').removeClass("className");
    

    to remove it and restore the element to its previous state. This way you can decorate your alements with a lot more “features” like background-color or font-style as well.

    Update: you can do something like:

        /** css: **/
        input {
          border:solid 1px;
        }
        .error {
           border-color:#f00;
        }     
    
    
        /** script: **/
    
                var resetForm = function () {
              $('#cl_txt, #comment_txt, #mks_txt').removeClass('error');
        }
        var renderError = function (elm, isValid) {
              if(!isValid){
                 elm.addClass('error');
              }
        }
        var validate = function (value, reg){
            return value.match(reg);
        }
    
        $('#mks_btn').click(function(e) {
                resetForm();
                var elm = $('#mks_txt');
                renderError(elm, validate(elm.val(), 
                                             /^(CL\s*)?[0-9]{6,}$/i))
                e.preventDefault();
        });
        $('#cl_btn').click(function(e) {
                resetForm();
                var elm = $('#cl_txt');
                renderError(elm, validate(elm.val(), 
                                /^(MKS\s*)?[0-9]{6,}$/i))
                e.preventDefault();
        });
        $('#comment_btn').click(function(e) {
    
                resetForm();
                var elm = $('#comment_txt');
                renderError(elm, validate(elm.val(),
                                 elm.val().length < 2))
                e.preventDefault();
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.