I am having a script which validates a user's name. When the user enters name without spaces then it works fine. If user enters name with spaces (name, surname, lastname etc) then it is not working. I am trying to capture 'space' character using \s but it is not working.
My script is:
var name=document.forms["newform"]["fullname"].value; //get the name from HTML form
var charpos = name.search("[^A-Za-z\s]");
if (name.length <= 0 || name.length>=50 || charpos >= 0)
{
alert('wrong.');
}
else
{
alert('correct');
}
Input1: AFGH
Output1: correct
Input2: ABCD EFGH
Output2: wrong
When Input2 is given, charpos gets the value '5'.