Jquery validate plugin number validation with special characters

16,009

Solution 1

You can accomplish this with your own custom rule for validator:

$.validator.addMethod("custom_number", function(value, element) {
    return this.optional(element) || value === "NA" ||
        value.match(/^[0-9,\+-]+$/);
}, "Please enter a valid number, or 'NA'");

This validates the field if it's optional, equal to "NA" or meets the regular expression.

And then you would just apply this new rule to whatever field you want:

$("form").validate({
    rules: {
        number: {
            required: true,
            custom_number: true,
            minlength: 4
        }
    }
});

Obviously, you should replace the name custom_number with something a bit more descriptive.

Check out a working example here: http://jsfiddle.net/andrewwhitaker/RtK2p/1/

Solution 2

Check this one http://archive.plugins.jquery.com/project/jvalidation more flexible and easy to use with less than 3 k size.

in your case you can use default data-validate-format validator

<input data-validate-format='/^([0-9,\+-]{0,4}|NA)$/' />

Check regex I put as value of the attribute

They have a sample for extending with optional validations such as numbers

$.fn.validations.options.validators.age = function(){
  return this.val()>0&&this.val()<100;
}

and in your html you add sth like

<input data-validate-age='true' data-validate-error='.err1' />
<div class='err1'>Age should be more than 0 and less than 100!</div>

Check Full Documentation for jQuery Validation Plugin

Share:
16,009
Alfred
Author by

Alfred

I am a Full Stack developer and a DevOps Engineer, who always to learn from, and contribute to, the technology community. I was a beginner in programming once. What all I had was pieces of scattered and crude knowledge (don't know if i can call it knowledge) then. Later, I joined for Master of Computer Applications in a college and there, I got a great teacher. It was her, who taught me when and where to use 'while' and 'for' loops even.What all knowledge I have now, and what all achievements I've made till now, is only because of her. Compared to her, I am ashes. Hats off my dear teacher Sonia Abraham, you are the best of your kind. Sonia Abraham is a professor of the Department of Computer Applications, M.A College of Engineering, Mahatma Gandhi University

Updated on June 04, 2022

Comments

  • Alfred
    Alfred almost 2 years

    I am using jquery validate plugin to validate a Number field in my form.

    The script for validation is

    NUMBER: {
        required: true,
            number: true,
        minlength: 4,
        },
    

    which validates my formfield <input name="NUMBER" type="text" id="NUMBER" value="" maxlength="4" >

    but, i want to include (or allow) special characters "+", "-", and ",", and word "NA" in my formfield. How can I make this possible?? The word "NA" should be validated when it is alone, without any other letter or number..

    Thanks in advance.. :)

    blasteralfred