Data validation by JavaScript (verify IP address, etc.)

Source: Internet
Author: User
Tags count empty range reset microsoft frontpage
Html>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta name= "generator" content= "Microsoft FrontPage 4.0" >
<meta name= "ProgId" content= "FrontPage.Editor.Document" >
<title>new Page 1</title>

<body>

<script language = ' JavaScript ' >
Check for English
function Checkchar ()
{
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for (i=0 i < document.form.aaa.value.length; i++)
{
var Checkchar = document.form.aaa.value.charAt (i);
Checkchar = Checkchar.touppercase ();
if (Letters.indexof (checkchar) = =-1)
{
Alert ("input type must be English!") ");
return false;
}
}
}

Checksum is a number

function Checknum (p)
{
if (p = = "")
{Alert ("Input cannot be empty!") ");
return false;
}
var L = p.length;
var count=0;
for (var i=0; i<l; i++)
{
var digit = P.charat (i);
if (digit = = ".")
{
++count;
if (count>1)
{Alert ("input type must be a number!") ");
return false;
}
}
else if (digit < "0" | | digit > "9")
{Alert ("input type must be a number!") ");
return false;
}
}
return true;
}

Verify IP address format
function Checkip ()
{
var scount=0;
var ip = document.form3.ccc.value;
var iplength = ip.length;
var letters = "1234567890.";
for (i=0 i < document.form3.ccc.value.length; i++)
{
var Checkchar = document.form3.ccc.value.charAt (i);
if (Letters.indexof (checkchar) = =-1)
{
Alert ("The IP address format is not!!, you can only enter numbers and". ", the format is XXX.XXX.XXX.XXX for example: 192.168.0.1");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}
}

for (var i = 0;i<iplength;i++)
(Ip.substr (i,1) = = ".")? Scount++:scount;
if (scount!=3)
{
Alert ("The IP address format is not correct!, you can only enter numbers and". ", the format is XXX.XXX.XXX.XXX for example: 192.168.0.1");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}

A/Ip.indexof (".");
last = Ip.lastindexof (".");
STR1 = ip.substring (0,first);
Subip = ip.substring (0,last);
Sublength = Subip.length;
Second = Subip.lastindexof (".");
str2 = subip.substring (First+1,second);
STR3 = subip.substring (second+1,sublength);
STR4 = ip.substring (last+1,iplength);

if (str1== "" | | str2== "" | | str3== "" | | STR4 = "")
{Alert ("Number cannot be empty!") Format for XXX.XXX.XXX.XXX For example: 192.168.0.1 ");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}
if (str1< 0 | | | str1 >255)
{Alert ("number range is 0~255! ");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}
else if (str2< 0 | | str2 >255)
{Alert ("number range is 0~255! ");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}
else if (str3< 0 | | str3 >255)
{Alert ("number range is 0~255! ");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}
else if (str4< 0 | | STR4 >255)
{Alert ("number range is 0~255! ");
Document.form3.ccc.value= "";
Document.form3.ccc.focus ();
return false;
}

Verifying phone numbers
function Checkphone ()
{
var letters = "1234567890,";
for (i=0 i < document.form4.ddd.value.length; i++)
{
var Checkchar = document.form4.ddd.value.charAt (i);
if (Letters.indexof (checkchar) = =-1)
{
Alert ("Phone number can only be a number or comma!") ");
return false;
}
}
}


}



</script>

<form name = "form" method= "POST" action = "AA" onsubmit = "return Checkchar ()" >
<p> Check English letters: <input type= "text" Name= "AAA" size= ">"
</p>
<p>
<input type= "Submit" value= "submitted" name= "B1" >
<input type= "reset" value= "rewrite" name= "B2" ></p>
</form>

<form name = "Form2" method= "POST" onsubmit= "return Checknum (Document.form2.bbb.value)" >
<p> Check Number: <input type= "text" Name= "BBB" size= "></p>"
<p><input type= "Submit" value= "submitted" name= "B1" ><input type= "reset" value= "all Rewrite" name= "B2" ></p>
</form>

<form name = "FORM3" method= "POST" onsubmit = "return Checkip ()" >
<p> Verify IP Address: <input type= "text" Name= "CCC" size= "></p>"
<p><input type= "Submit" value= "submitted" name= "B1" ><input type= "reset" value= "all Rewrite" name= "B2" ></p>
</form>

<form name = "FORM4" method= "POST" onsubmit = "return Checkphone ()" >
<p> Check phone number: <input type= "text" name= "ddd" size= "><input type=" Submit "value=" submitted "name=" B1 ">< Input type= "reset" value= "rewrite" name= "B2" ></p>
</form>

</body>



Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.