0

I wrote a javascript validation script to validate if the URL contains '+' or whitespaces.

$.validator.addMethod("reportFileURL",
                    function(value,element){
                        if(((value.search(/[+]+/)) == -1) 
                                  && ((value.search(/[ ]+/))) == -1){
                           return true;
                        }
                     },"Invalid filename");

Can i write the script more precise than this.

1

2 Answers 2

2
if (value.search(/[\s+]/) == -1) {
    return true;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanx Michael it works...:) By the way can you suggest me a book or website to use regex for javascript form validation.
@SangramAnand glad it was helpful :) I use regex.larsolavtorvik.com to test my regular expressions of JS & PHP
If you want to target all whitespace (and not just the space character), you should change the regex to /[\s+]/.
1
return value.search(/[ +]/) == -1;

Comments

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.