//Remove the spaces on both sides of the input string
function trim(s) {
var count = s.length;
var st = 0; // start
var end = count-1; // end
if (s == "") return s;
while (st < count) {
if (s.charAt(st) == " ")
st ;
else
break ;
}
while (end > st) {
if (s.charAt(end) == " ")
end --;
else
break;
}
return s.substring(st,end 1);
}
If the form is like this:
To determine whether the input is empty, you can define the function like this:
function isEmpty(){
//form1 is the name attribute in form
var _form = document.form1;
if (trim(_form.name.value)==""){
alert("Username cannot be empty!");
return false;
}
if(trim(_form.pwd .value)==""){
alert("Password cannot be empty!");
return false;
}
return true;
}