Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Validate SSN in jQuery

Hi Experts,
I am developing .ASPX Web Page in VS2003.  I need to validate SSN in jQuerey.  Any idea?  Thanks in advance.
Avatar of Venabili
Venabili
Flag of Bulgaria image

I am usually using http://digitalbush.com/projects/masked-input-plugin/ when I need something like this...
And then something similar to http://stackoverflow.com/questions/982027/jquery-validate-and-grouping-similar-form-items for the validations...

Alternatively, just use a regex: ^\d{3}-\d{2}-\d{4}$
And of course - why do you need it in jQuery? That's client side validation so why not make in Javascript directly? http://javascript.internet.com/forms/val-ssn.html for example
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Hi Venabilli,
I need it in jQuery as all other validations are in jQuere.  I tried your 1st suggestion.  It did not work for me.  In fact nothing happened when hit submit button.  Probably it is not able to find the jQuery plugin libraries required for validations.  In fact I need a simple example which should work.  Thank you very much for your help though.
How do you do your validations? Do you use http://bassistance.de/jquery-plugins/jquery-plugin-validation/ ?

If you do,  http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation has quite a good discussion on how to implement a regex one and I  gave you a regex to use above.

If not - can you show how you do your validations?
At this point I don't need to use the validation any more.  I will update you when I need it again.
So what did you end up using?

You cannot just dismiss a question because you do not need it -- especially after leaving it hanging for 2 months
Venabili:
I am not dismissing it, but did not find any specific reply so far, and at the same time my priority changed little bit.  Now I am back.  I looked at the link http://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation.  It talks about some custom rules, which is very difficult to follow.  It doesn't talk anything about SSN validation of in jQuery.  My question is very simple.  Is it possible for you to provide me a simply resolution?

Thanks.
Avatar of leakim971
Hi leakim971,
Yes, It is something like this.  But I tested the code, which is not working.  Looks like there is problem in the mask.  When I comment it out, it works, but doesn't validate the SSN in its format.  Here is the code below, please take a look if you can.  Thanks.

<HTML>
      <HEAD>
      <title>Demo2</title>

    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js" type="text/javascript"></script>
 
      <script language="javascript">
            $.validator.setDefaults({
                  submitHandler: function() { alert("submitted!"); }
            });

            $().ready(function() {
                  $("#ssn").mask("999-99-9999");
                  $("#myForm").validate({
                    rules: {
                         name: "required",
                           ssn: "required"
                        },
                        messages: {
                              name: "Please enter your name!",
                           ssn: "Please enter your ssn!"
                      }
                  });
            });
      </script>

      <form id="myForm">
            <fieldset>
                  <legend>
                        My Sample Form</legend>
                  <label><strong>Name:</strong></label><br>
                  <input name="name" id="name">
                  <br>
                  <label><strong>SSN:</strong></label><br>
                  <input name="ssn" id="ssn">
                  <br>
                  <input class="submit" type="submit" value="submit">
            </fieldset>
      </form>

</body>
</HTML>
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you leakim971 for your help.  This works great.  Many many thanks to you.  I think I can close this case now.