Jquery validates plugin numbers with special characters

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

script to check

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

which checks my form field <input name="NUMBER" type="text" id="NUMBER" value="" maxlength="4" >

but I want to include (or allow) the special characters " +", " -" and " ," and the word " NA" in my form field. How can I make this possible? The word " NA" should be checked if it is one, without any other letters or numbers.

Thanks in advance..:)

blasteralfred

+3
source share
2 answers

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

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

, , "NA" .

:

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

, custom_number - .

: http://jsfiddle.net/andrewwhitaker/RtK2p/1/

+7

http://archive.plugins.jquery.com/project/jvalidation 3 .

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

,

,

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

html 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>

jQuery

+1

Source: https://habr.com/ru/post/1787462/


All Articles