How can I apply this trim function: var trimmed = str.replace(/^\s+|\s+$/g, '') ; to this code below so that if a textbox contains whitespaces, it will display message “Incorrect”? Also If I use this function am I able to use “/” in the textbox because the textbox may contain data with “/”.
if (moduletext.value == ""){
errmsg.innerHTML = "Incorrect";
}else{
errmsg.innerHTML = "Correct";
}
The easiest way to do what you’re trying to do is check the trimmed value’s length.
If you know what browser you’re using (generally anything not <IE9), you may be able to use the native browser implementation of trim, i.e.
'my string'.trim();