Adrian
Adrian

Reputation: 353

jQuery validate regex to disallow numerics and special characters

I have a form that requires a field to not have any special characters or numerics. Currently it works fine with:

The issue is when I add something like #$%abc or abc123 it does not produce the the error I would expect.

The function I am using looks like:

$.validator.addMethod("checkallowedchars", function (value) {
    var pattern = new RegExp('[A-Za-z]+', 'g');
    return pattern.test(value)
}, "The field contains non-admitted characters");

JSFiddle shows the function/regex I am using: http://jsfiddle.net/nmL8maa5/

Upvotes: 3

Views: 3462

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

One of the correct answers is:

return /^[A-Z]+$/i.test(value);

and add

checkallowedchars: true,

to the rules.

See the updated demo fiddle (the one below does not work for me on SO, no idea why).

$(document).ready(function () {
    
    $.validator.addMethod("pwcheckallowedchars", function (value) {
        return /^[a-zA-Z0-9!@#$%^&*()_=\[\]{};':"\\|,.<>\/?+-]+$/.test(value) // has only allowed chars letter
    }, "The password contains non-admitted characters");

    $.validator.addMethod("checkallowedchars", function (value) {
        return /^[A-Z]+$/i.test(value);
    }, "The field contains non-admitted characters");

    $.validator.addMethod("pwcheckspechars", function (value) {
        return /[!@#$%^&*()_=\[\]{};':"\\|,.<>\/?+-]/.test(value)
    }, "The password must contain at least one special character");
    
    $.validator.addMethod("pwcheckconsecchars", function (value) {
        return ! (/(.)\1\1/.test(value)) // does not contain 3 consecutive identical chars
    }, "The password must not contain 3 consecutive identical characters");

    $.validator.addMethod("pwchecklowercase", function (value) {
        return /[a-z]/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckrepeatnum", function (value) {
        return /\d{2}/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckuppercase", function (value) {
        return /[A-Z]/.test(value) // has an uppercase letter
    }, "The password must contain at least one uppercase letter");
    
    $.validator.addMethod("pwchecknumber", function (value) {
        return /\d/.test(value) // has a digit
    }, "The password must contain at least one number");
    
    
    
    
    
    $('#myform').validate({
        // other options,
        rules: {
            "firstname.fieldOne": {
                required: true,
                checkallowedchars: true,
                pwchecklowercase: true,
                pwcheckuppercase: true,
                pwchecknumber: true,
                pwcheckconsecchars: true,
                pwcheckspechars: true,
                pwcheckallowedchars: true,
                minlength: 8,
                maxlength: 20
            }
        }
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.js"></script>
<form id="myform">
    <input type="text" name="firstname.fieldOne" /><br/>
    <br/>
    <input type="submit" />
</form>

There are two issues in your code:

  • The regex is not anchored, and when it is not anchored it matches partial substrings inside larger strings. Since it just matches 1+ letters, and #$%abc contains 3 letters, the condition is met.
  • The second issue is that your are using /g modifier wth RegExp.text(). This leads to unexpected behavior.

Upvotes: 1

Aminah Nuraini
Aminah Nuraini

Reputation: 19146

Use this:

^[A-Za-z]+$

Explanation:

  1. ^ is the string beginning anchor.
  2. $ is the string end anchor.

You should use it for the regex to process the string from the beginning to the end, not including the middle.

Upvotes: 0

Related Questions