Copy the code code as follows:
//Remove 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 looks like this:
Copy the code code as follows:
<form action="testnew.html" name="form1">
username: <input type="text" name="name">
password: <input type="password" name="pwd"> <br>
<input type="submit" value="Submit" onclick="isEmpty()">
</form>
Then to determine whether the input is empty, you can define the function like this:
Copy the code code as follows:
function isEmpty(){
//form1 is the name attribute in the 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;
}