JavaScript Form Validation
JS Error
Introduction to DOM
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
JavaScript Form Validation
JavaScript can be used to validate these input data in an HTML form before the data is sent to the server.
These typical forms of data that are validated by JavaScript are:
Has the user filled out the required fields in the form?
is the email address entered by the user legal?
Has the user entered a valid date?
Does the user enter text in the Data field (numeric field)?
Required (or required) items
The following function is used to check whether the user has filled out the required (or required) items in the form. If the required or required option is empty, the warning box pops up, and the return value of the function is false, otherwise the return value of the function is true (meaning there is no problem with the data):
function validate_required (field,alerttxt)
{
With (field)
{
if (value==null| | value== "")
{alert (alerttxt); return false}
else {return true}
}
}
Here's the code along with the HTML form:
<script type= "Text/javascript" >
function validate_required (field,alerttxt)
{
With (field)
{
if (value==null| | value== "")
{alert (alerttxt); return false}
else {return true}
}
}
function Validate_form (thisform)
{
With (Thisform)
{
if (validate_required email, "Email must be filled out!") ==false)
{Email.focus (); return false}
}
}
</script>
<body>
<form action= "submitpage.htm" onsubmit= "return Validate_form (This)" method= "POST" >
Email: <input type= "text" name= "email" size= ">"
<input type= "Submit" value= "Submit" >
</form>
</body>
E-mail verification
The following function checks whether the input data conforms to the basic syntax of an e-mail address.
This means that the data entered must contain the @ symbol and the dot (.). At the same time, @ cannot be the first character of the email address and must have at least one dot after @:
function Validate_email (field,alerttxt)
{
With (field)
{
Apos=value.indexof ("@")
Dotpos=value.lastindexof (".")
if (apos<1| | DOTPOS-APOS<2)
{alert (alerttxt); return false}
else {return true}
}
}
Here is the complete code along with the HTML form:
<script type= "Text/javascript" >
function Validate_email (field,alerttxt)
{
With (field)
{
Apos=value.indexof ("@")
Dotpos=value.lastindexof (".")
if (apos<1| | DOTPOS-APOS<2)
{alert (alerttxt); return false}
else {return true}
}
}
function Validate_form (thisform)
{
With (Thisform)
{
if (validate_email (email, "Not a valid e-mail address!") ==false)
{Email.focus (); return false}
}
}
</script>
<body>
<form action= "submitpage.htm" onsubmit= "return Validate_form (this);" method= "POST" >
Email: <input type= "text" name= "email" size= ">"
<input type= "Submit" value= "Submit" >
</form>
</body>
Complete a more complete user registration page.
1: The user is required to enter the user name can only input English, numbers and underline
2: Require the user to enter the password and Confirm password must be consistent
3: Ask the user to upload a picture file on the local disk as the Avatar
4: Require the user to enter a verification mailbox, verify that the mailbox format is correct through JavaScript code
5: Require the page to implement the Verification code function, click the "Register" button, whether or not to complete the registration, the verification code can be automatically refreshed
<script type= "Text/javascript" >
function Validate_email (field,alerttxt)
{
With (field)
{
Apos=value.indexof ("@")
Dotpos=value.lastindexof (".")
if (apos<1| | DOTPOS-APOS<2)
{alert (alerttxt); return false}
else {return true}
}
}
function Validate_form (thisform)
{
With (Thisform)
{
if (validate_email (email, "Not a valid e-mail address!") ==false)
{Email.focus (); return false}
}
}
</script>
<body>
<form action= "submitpage.htm" onsubmit= "return Validate_form (this);" method= "POST" >
Email: <input type= "text" name= "email" size= ">"
<input type= "Submit" value= "Submit" >
</form>
</body>
javascript--form verification, e-mail verification