1

I have an optional field (say "text1") which may either be blank or only alpha-numeric:

jQuery.validator.addMethod("onlyAlphaNumeric", 
        function(value, element) {  
            var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
            return ((this.optional(element)) || regExp.test(value));
        }
    , "Only aplaha-numeric characters allowed");


$("#student-search-form").validate({
    rules : {
        text1 : {
            optional : true,
            onlyAlphaNumeric: "Only a-n allowed"
        }
    },
    messages: {
        text : {                    
            acceptOnly: " Only alpha-numeric characters allowed"
        }               
    }
});

The problem is no validation happens, so if user enters "!&^%(*" in 'text1', the form gets submitted, no error checks.

Can somebody please tell me what am I doing wrong? Thank you.

Sparky
  • 98,165
  • 25
  • 199
  • 285
kmansoor
  • 4,265
  • 9
  • 52
  • 95

3 Answers3

2

This is wrong...

rules : {
    text1 : {
        optional : true,
        onlyAlphaNumeric: "Only a-n allowed"
    }
},

For onlyAlphaNumeric:, you can only put true or false.

I am not too sure about optional: but I know required: is valid... so set required: to false. Alternatively, you could probably leave it out entirely since the validator defaults to fields as being "optional" (required:false) unless you specify otherwise.

rules : {
    text1 : {
        required : false,
        onlyAlphaNumeric: true
    }
},

See this similar answer...

using the jquery validation plugin, how can I add a regex validation on a textbox?

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • `required` is false by default, so no need to state it (unless it's for clarity's sake). And, after it's all said and done... [you have this](http://jsfiddle.net/DvC8w/). – Brad Christie Oct 06 '11 at 17:43
  • @BradChristie, I thought so, and I already stated something like that above. _"Alternatively, you could probably leave it out entirely since the validator defaults to fields as "optional" unless you specify otherwise."_ – Sparky Oct 06 '11 at 17:50
  • Thank you, a couple of things were wrong (as pointed), required : false instead of optional : true (or no need for required : false at all, as it's the default) and as @Sparky672 mentioned, "onlyAlphaNumeric:, you can only put true or false." – kmansoor Oct 06 '11 at 20:36
0

you can add optional to the validation method -

http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

jQuery.validator.addMethod("alphanumeric", function(value, element) {
    return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, spaces or underscores only please");

Validate -

rules : {
     text1 : {
         alphanumeric: true
     }
 },
Jayendra
  • 52,349
  • 4
  • 80
  • 90
  • yup \w would include some others as well. This is an example of using jquery with optional validation. need to be customized as per needs. – Jayendra Oct 06 '11 at 17:38
  • Also interesting the author uses the i flag for a pattern synonymous with `[0-9a-zA-Z_]` – Brad Christie Oct 06 '11 at 17:39
  • yup .. was checking on the same thing. \w should cover all cased characters. Need to check for any specific reason i was included. – Jayendra Oct 06 '11 at 17:47
0

I think that your code must be something like this :

// Suppose that your method is well defined
jQuery.validator.addMethod("onlyAlphaNumeric", 
    function(value, element) {  
        var regExp = new RegExp(/^[a-zA-Z0-9]+$/);              
        return ((this.optional(element)) || regExp.test(value));
    }
, "Only alpha-numeric characters allowed");

$("#student-search-form").validate({
    rules : {
        text1 : {
        required : false, // optional must be replaced by required
        onlyAlphaNumeric: true // rules are boolean
        }
    },
    messages: {
        text1 : {
            onlyAlphaNumeric: "I can change my message : Only alpha-numeric characters allowed"
        }
    }
});
Nabil SADKI
  • 134
  • 3