As valid by jquery that my message field does not contain a space?

4

I want to validate my textarea so that you do not write a blank space at the beginning

if( $("#comments").val() == ""  || $("#comments").attr("value").match(/^/s+$/)){
    $("#comments").focus().after("<span class='error'>This field is required.</span>");
    return false;
}
    
asked by AitorUdabe 27.10.2016 в 12:52
source

1 answer

2

You are putting the bar upside down. In regular expressions the bar \ in front of a character indicates that that character will have a special meaning instead of its literal meaning (for example \n does not mean the character n but the new line).

So, in your regular expression it should be \ instead of /, and it would be something like this /^\s+$/ , so the code would look like this:

if( $("#comments").val() == ""  || $("#comments").attr("value").match(/^\s+$/)){
    $("#comments").focus().after("<span class='error'>This field is required.</span>");
    return false;
}
    
answered by 27.10.2016 / 13:08
source