3

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

  • abc: no error.
  • 123: error.
  • !@#$: error.

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/

3
  • use anchors :- ^[A-Za-z]+$ Commented May 2, 2016 at 7:30
  • return /^[A-Za-z]+$/.test(value) Commented May 2, 2016 at 7:31
  • @Adrian, please check my answer below, and please let me know if anything is still unclear. Commented May 2, 2016 at 12:38

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Just sharing the pattern is not enough. There is an issue related to the regex/code.
Yup, he uses g flag. I don't know why is that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.