1

I am using this home-made function to validate text fields, but for some reason, it doesn't "accept" spaces. I find that weird, since I have \s in my class...

function validateText(controlid, minlength, maxlength, required) {
    var control = document.getElementById(controlid);
    if (!required && control.value.length == 0) control.style.backgroundColor = "White";
    else {
        var regex = new RegExp("^[a-zA-Z0-9\(\)\.\s_,:/-]{" + minlength + "," + maxlength + "}$", "g");
        if (!regex.test(control.value))
            control.style.backgroundColor = "#FFDDDD";
        else
            control.style.backgroundColor = "White";
    }
}

Can you tell me why entering a space turns the textbox red? Thanks :)

2
  • What is the minlength and maxlength values in that case Commented Jul 28, 2011 at 3:31
  • @Jesper - I made a demo, and working fine. hope some help ~ http://jsfiddle.net/5ryx8/ Commented Jul 28, 2011 at 4:18

1 Answer 1

3

I believe it's because you're trying to put \s in a class. Inside a class (eg, []) \s is simple a badly-escaped "s". Either use a literal space, or do ^([...]|\s){.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, if you replace the \s with a space, it works. You can see it work here in the jsFiddle: jsfiddle.net/jfriend00/Ne6zC.

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.