Create a social security validation for jquery validator plugin

I am using this plugin:
http://docs.jquery.com/Plugins/Validation

And I would like to create a custom rule that validates for US social security numbers. (###-##-####), I know it can be done, I have done stuff like this in the past but I am on deadline and I don't have time to figure it out.

If I could just use a regex as a validation rule that would be cool since I am pretty good with that.

Thanks!
skioneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nap0leonCommented:
\Since you are already using the plugin, and already good at regex, this should be a snap for you!

http://corpocrat.com/2009/07/15/quick-easy-form-validation-tutorial-with-jquery/

Yours could be identical  to the example for "To validate Phone Number" - except for the error message, of course.
0
skioneAuthor Commented:
I want to use the plugin since the form has a lot of fields and they are all already being used with this plugin. I'd rather not mix plugins
0
nap0leonCommented:
Is the tutorial I linked for a different plugin with the same name?
0
skioneAuthor Commented:
Upon review it is the same plugin but my question was how to write this specific validation rule, not provide me with a link. I don't mean to be snarky but I could of RTFW'ed without asking you to tell me to do it.
0
nap0leonCommented:
It took you longer to type your comment than to get the code and make the necessary adjustments from "phone" to "SSN".  The only "coding" on your part is updating the regex that you said you are good at, so yes, I linked to the source rather than adjusting it for you - especially since the edits to make it say "SSN" instead of phone are the same edits you will need to make for it to use your field ID and your error message...

<script>
  $(document).ready(function(){
    $.validator.addMethod("NumbersOnly", function(value, element) {
        return this.optional(element) || /^[0-9\-]+$/i.test(value);
    }, "SSN must contain only numbers and -.");
 
    $("#regForm").validate();
  });
  </script>

Open in new window


and now you can validate specifying class=”required NumbersOnly”. This will allow only numbers 0-9, and a hyphen.
<tr>
  <td>SSN</td>
  <td><input name="SSN" type="text" id="SSN" class="required NumbersOnly"></td>
</tr>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.