Javascript| Web page As a web designer, a coder, are you tired of the form verification question in web design
Problem? The most tireless copy
if(x) {alert (' wrong ');}, or the use of a difficult and I mean
Ide? Well, let me tell you a more lazy way of writing, you might not be able to verify the form so much
.....
Principle:
Form validation is all about verifying that you want to collect each piece of information, which means writing a
Frmvalid JavaScript function, in which you perform the following actions:
...
if(The item to be validated is not eligible)
{
Alert' It's a mistake! ');
The entry to be validated. focus ();
Return false;
}
...
// All right
Return true;
Of course,<.form...onsubmit=' return frmvalid () '> must be included in
In the HTML code. Think about it, the more you want to verify the more code, the easier it will be to make mistakes.
Here we put the validation entries in an array, as follows:
Elemarray =New Array(
' to be verified entry name ',
' Validation criteria ',
' Error prompt ');
Then the validation code will be greatly streamlined, and we can achieve this verbose by using loops as follows
The code is now functional, here we use withAndEvalStatement constructs the judgment condition:
with(Eval (' obj. '+elems[i][0]))
{
if(eval (elems[i][1]))
{
window. alert (elems[i][2]);
Focus ();
Return false;
}
}
We can iterate through each entry by building a multiple-bit array:
Elems =New Array(
New arrary( ...),
...
);
for(i = 0; i < elems.length; i++)
{
The verification statement above
}
Actual combat:
1, use the following example to write a validation script:
<script language="JavaScript"Type="Text/javascript">
//
Function:frmvalid
// ------------------
Author Hongz
Usage:yourform..
Purpose:to validate form elements in a integrated way.
//
function frmvalid (obj)
{
Elements array, initialization for validation
elems = new Array(
New Array(
' username ', Name of elements to is validated
' value.length<1 | | value.search (/[^a-za-z0-9_]/) >=0 ',
//Validation condition
' Invalid Username: only 6-20-digit letters, numbers, underline combinations! '),
//prompt On Failure
New Array(
' Password ',
' value.length<5 | | value.search (/[^a-za-z0-9_]/) >=0 ',
' Invalid password: only 6-20-digit letters, numbers, and underline combinations can be entered! '),
New Array(
' Email ',
' IsMail (value) ==false ',
' Email is your important contact tool on the Internet, please fill it out correctly! ')
);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.