1

I was wondering if someone could help me using the JQuery validation plugin. I need to add the ability to make sure that a field only contains letters, numbers, underscores, whitespacke, and dashes. I currently have the following js code (not including the call to jquery and the plugin .js files)

$(document).ready(function() {
$("#info_frm").validate();

});

function submitForm() {
    if( $("#info_frm").valid() ) {
        document.info_frm.submit();
    }
    return false;
}
Splashlin
  • 7,225
  • 12
  • 46
  • 50

3 Answers3

2

you could add a method depending on the what rules you want. for example:

$.validator.addMethod(
        "numeric", 
        function(value, element) {
            var regex=/^[0-9]+$/; //this is for numeric... you can do any regular expression you like...
            return this.optional(element) || regex.test(value);
        },
        "Only numeric please."
);

Try looking here for more info.

Community
  • 1
  • 1
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
1

Just use a regex method to validate your field:

$.validator.addMethod(
    "namecheck", function(value) {
        return /^[-_\sa-zA-Z0-9]*$/.test(value);
    }, "This name is not correct"
);

now you can add the rule:

$("[name='yourfieldname']").rules("add", { namecheck: true })

or

<input type="text" class="required namecheck" name="yourfieldname" />
RJD22
  • 10,230
  • 3
  • 28
  • 35
  • FYI: ("yourfieldname").rules("add", { namecheck: true }) lacks $ in it... should have been $("yourfieldname").rules("add", { namecheck: true }) – Reigel Gallarde Jan 27 '10 at 09:12
  • moreover, $("yourfieldname") is not a valid selector. it is def not a name selector – mkoryak Jul 15 '11 at 13:36
0

check out my validation plugin, it allows for the following:

  • Required Field Validation
  • Regular Expression Validation
  • Email Validation
  • Numeric Validation
  • Conditional Validation

You can check it out here.

REMEMBER Client side validation isn't enough. You must also validate on the server.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Jeremy Boyd
  • 5,245
  • 7
  • 33
  • 57