I'm trying to very that the user actually entered something in the form, and not just spaces.
I guess the problem is in the first line of isBlank() function. I've tried the following:
I found the trim functions somewhere on the web, so I'm guessing they are probably fine.
Here's the code:
I guess the problem is in the first line of isBlank() function. I've tried the following:
- elem.value.trim ();
- elem.value=elem .value.trim();
- elem.value=trim (elem.value);
- elem.value.repl ace(/^\s+|\s+$/g,"");
I found the trim functions somewhere on the web, so I'm guessing they are probably fine.
Here's the code:
Code:
function check_myForm(){
if(isBlank(myForm.ofc, 'Please enter ofc:')){
return false;
}
}
function isBlank(elem, helperMsg) {
elem.value.replace(/^\s+|\s+$/g,"");
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return true;
}
return false;
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}function ltrim(str) {
for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
return str.substring(k, str.length);
}
function rtrim(str) {
for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
return str.substring(0,j+1);
}
function trim(str) {
return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
var whitespaceChars = " \t\n\r\f";
return (whitespaceChars.indexOf(charToCheck) != -1);
}
Comment