Home > Web Front-end > JS Tutorial > JS determines whether the form input is empty (sample code)_javascript skills

JS determines whether the form input is empty (sample code)_javascript skills

WBOY
Release: 2016-05-16 17:07:39
Original
1204 people have browsed it

Copy code The code is as follows:

//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:
Copy the code The code is as follows:

username:

password:



To determine whether the input is empty, you can define the function like this:
Copy code The code is as follows:

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;

}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template