This example summarizes common validation functions for JavaScript. Share it for your reference. The specific summary is as follows:
1. String class verification
1. Length limit
Copy the code as follows:<script>
function test()
{
if(document.abvalue.length>50)
{
alert("cannot exceed 50 characters!");
document.abfocus();
return false;
}
}
</script>
<form name=a onsubmit="return test()">
<textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
<input type="submit" name="Submit" value="check">
</form>
2. It can only be Chinese characters
Copy the code code as follows: <input onkeyup="value="/oblog/value.replace(/[^/u4E00-/u9FA5]/g,'')">
3." can only be in English
Copy the code as follows: <script language=javascript>
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>
<input onkeydown="onlyEng();">
4. It can only be a number
Copy the code as follows: <script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//Consider the numeric keys on the keyboard
event.returnvalue=false;
}
</script>
<input onkeydown="onlyNum();">
5. Only English characters and numbers
Copy the code code as follows: <input onkeyup="value="/oblog/value.replace(/[/W]/g,"'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">
6. Verify the email format
The code copy is as follows: <SCRIPT LANGUAGE=javascript RUNAT=Server>
function isEmail(strEmail) {
if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/) != -1)
return true;
else
alert("oh");
}
</SCRIPT>
<input type=text onblur=isEmail(this.value)>
7. Block keywords (block *** and **** here)
Copy the code as follows:<script language="javascript1.2">
function test() {
if((abvalue.indexOf ("***") == 0)||(abvalue.indexOf ("****") == 0)){
alert("123");
abfocus();
return false;}
}
</script>
<form name=a onsubmit="return test()">
<input type=text name=b>
<input type="submit" name="Submit" value="check">
</form>
8. Whether the password is the same twice
Copy the code code as follows: <FORM METHOD=POST ACTION="">
<input type="password" id="input1">
<input type="password" id="input2">
<input type="button" value="test" onclick="check()">
</FORM>
<script>
function check()
{
with(document.all){
if(input1.value!=input2.value)
{
alert("false")
input1.value = "";
input2.value = "";
}
else document.forms[0].submit();
}
}
</script>
9. It’s cool to block the right click!
Copy the code as follows: oncontextmenu="return false" ondragstart="return false" onselectstart="return false"
Added in body
2. Form verification
1 The form item cannot be empty
Copy the code as follows:<script language="javascript">
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("Please enter your name!");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>
2 Compare whether the values of the two form items are the same
Copy the code as follows:<script language="javascript">
<!--
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) {
alert("The password you entered twice is different! Please re-enter.");
document.ADDUser.PWD.focus();
return false;
}
return true;
}
-->
</script>
3 The form item can only be number and "_", used for phone/bank account verification, can be extended to domain name registration, etc.
Copy the code as follows:<script language="javascript">
<!--
function isNumber(String)
{
var Letters = "1234567890-"; //You can increase the input value yourself
var i;
var c;
if(String.charAt( 0 )=='-')
return false;
if( String.charAt( String.length - 1 ) == '-' )
return false;
for( i = 0; i < String.length; i ++ )
{
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()
{
if(! isNumber(document.form.TEL.value)) {
alert("Your phone number is illegal!");
document.form.TEL.focus();
return false;
}
return true;
}
-->
</script>
4 Form entry value/length limit
Copy the code as follows:<script language="javascript">
<!--
function CheckForm()
{
if (document.form.count.value > 100 || document.form.count.value < 1)
{
alert("The input value cannot be less than zero or greater than 100!");
document.form.count.focus();
return false;
}
if (document.form.MESSAGE.value.length<10)
{
alert("Input text is less than 10!");
document.form.MESSAGE.focus();
return false;
}
return true;
}
//-->
</script>
5 Chinese/English/Number/Email Address Legality Judgment
Copy the code as follows: <SCRIPT LANGUAGE="javascript">
<!--
function isEnglish(name) //English value detection
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return false;
}
return true;
}
function is Chinese(name) //Chinese value detection
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return true;
}
return false;
}
function isMail(name) // E-mail value detection
{
if(! isEnglish(name))
return false;
i = name.indexOf(" at ");
j = name dot lastIndexOf(" at ");
if(i == -1)
return false;
if(i != j)
return false;
if(i == name dot length)
return false;
return true;
}
function isNumber(name) //Numerical detection
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charAt(i) < "0" || name.charAt(i) > "9")
return false;
}
return true;
}
function CheckForm()
{
if(! isMail(form.Email.value)) {
alert("Your email is illegal!");
form.Email.focus();
return false;
}
if(! isEnglish(form.name.value)) {
alert("The English name is illegal!");
form.name.focus();
return false;
}
if(! isChinese(form.cnname.value)) {
alert("The Chinese name is illegal!");
form.cnname.focus();
return false;
}
if(! isNumber(form.PublicZipCode.value)) {
alert("Zip code is illegal!");
form.PublicZipCode.focus();
return false;
}
return true;
}
//-->
</SCRIPT>
6 Characters that are not allowed to enter in a form item
Copy the code as follows:<script language="javascript">
<!--
function contains(str,charset)// string contains test functions
{
var i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
}
function CheckForm()
{
if ((contain(document.form.NAME.value, "%/(/)><")) || (contain(document.form.MESSAGE.value, "%/(/)><")))
{
alert("Illegal character entered");
document.form.NAME.focus();
return false;
}
return true;
}
//-->
</script>
3. Other verifications:
1. Check whether a string is composed of numbers
Copy the code code as follows:<script language="Javascript"><!--
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>
2. How to determine whether it is a character
The code copy is as follows: if (/[^/x00-/xff]/g.test(s)) alert("Contains Chinese characters");
else alert("all characters");
3. How to determine whether Chinese characters are included
The code copy is as follows: if (escape(str).indexOf("%u")!=-1) alert("Contains Chinese characters");
else alert("all characters");
4. Email format verification
Copy the code as follows: //Function name: chkemail
//Function introduction: Check whether it is an Email Address
// Parameter description: string to check
//Return value: 0: Not 1: Yes
function chkemail(a)
{ var i=a.length;
var temp = a.indexOf('@');
var tempd = a.indexOf('.');
if (temp > 1) {
if ((i-temp) > 3){
if ((i-tempd)>0){
return 1;
}
}
}
return 0;
}
5. Digital format verification
Copy the code as follows: //Function name: fucCheckNUM
//Function introduction: Check whether it is a number
//Particle description: Number to check
//Return value: 1 is a number, 0 is not a number
function fucCheckNUM(NUM)
{
var i,j,strTemp;
strTemp="0123456789";
if ( NUM.length== 0)
return 0
for (i=0;i<NUM.length;i++)
{
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//It means that there are characters but not numbers
return 0;
}
}
//The description is a number
return 1;
}
6. Phone number format verification
Copy the code as follows://function name: fucCheckTEL
//Function introduction: Check whether it is a phone number
// Parameter description: string to check
//Return value: 1 is legal, 0 is illegal
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i<TEL.length;i++)
{
j=strTemp.indexOf(TEL.charAt(i));
if (j==-1)
{
//It means that there are illegal characters
return 0;
}
}
//Explanation legal
return 1;
}
7. A function to determine whether the input is a Chinese language
Copy the code as follows: function ischinese(s){
var ret=true;
for(var i=0;i<s.length;i++)
ret=ret && (s.charCodeAt(i)>=10000);
return return;
}
8. A comprehensive function to judge the legitimacy of user input
Copy the code as follows:<script language="javascript">
//Limit the number of bits of input characters to start
//m is the user input, n is the number of digits to be limited
function issmall(m,n)
{
if ((m<n) && (m>0))
{
return(false);
}
else
{return(true);}
}
9. Determine whether the password is entered consistently
Copy the code as follows: function issame(str1,str2)
{
if (str1==str2)
{return(true);}
else
{return(false);}
}
10. Determine whether the user name is a number letter sliding line
The code copy is as follows: function notchinese(str){
var reg=/[^A-Za-z0-9_]/g
if (reg.test(str)){
return (false);
}else{
return(true); }
}
11. General check function for form text field
Function: Detect all input texts that must be non-empty, such as name, account, email address, etc.
This verification is now only for text fields. If you want to target other domain objects in form, you can change the judgment conditions.
How to use: Add title text to the text field to be detected. The text is a prompt message, the Chinese name of the field you want to prompt to the user. For example, to detect username
The html is as follows <input name="txt_1">. Of course, it is best to use visualization tools such as dreamweaver to edit the domain.
If you want to detect numeric type data, then unify the id of the domain into sz.
It is troublesome to judge date types in JavaScript, so there is no program for date type verification. Experts can add it.
The program is relatively grass, just provides an idea. Throwing bricks and attracting jade! :)
Oh, by the way, function call method: < form onsubmit="return dovalidate()">
Copy the code as follows: function dovalidate()
{
fm=document.forms[0] //Only one form is detected, if there are multiple forms, the judgment conditions can be changed
for(i=0;i<fm.length;i++)
{
//The detection and judgment conditions can be modified according to the type
if(fm.tagName.toUpperCase()=="INPUT" &&fm.type.toUpperCase()=="TEXT" && (fm.title!=""))
if(fm.value="/blog/="")//
{
str_warn1=fm.title+"cannot be empty!";
alert(str_warn1);
fm.focus();
return false;
}
if(fm.id.toUpperCase()=="SZ")//Digital verification
{
if(isNaN(fm.value))
{ str_warn2=fm.title+"Incorrect format";
alert(str_warn2);
fm.focus();
return false;
}
}
}
return true;
}
12. Verify that radio is selected
Copy the code as follows:<script language="javascript">
function checkform(obj)
{
for(i=0;i<obj.oo.length;i++)
if(obj.oo[i].checked==true) return true;
alert("Please select")
return false;
}
</script>
<form id="form1" name="form1" method="post" action="" onsubmit="return checkform(this)">
<input type="radio" name="oo" value="radiobutton" />
<input type="radio" name="oo" value="radiobutton" />
<input type="submit" name="Submit" value="Submit" />
</form>
I hope that the description in this article will be helpful to everyone's web programming based on javascript.