1. Only numbers and English can be entered:
<input onkeyup= "Value=value.replace (/[/w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') "id=" Text1 "name=" Text1 ">
2. Only numbers can be entered:
<input onkeyup= "Value=value.replace (/[^/d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') "id=" Text2 "name=" Text2 ">
3. Only the full angle can be entered:
<input onkeyup= "Value=value.replace (/[^/uff00-/uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/uff00-/uffff]/g, ') "id=" Text3 "name=" Text3 ">
4. Only Chinese characters are entered:
<input onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/u4e00-/u9fa5]/g, ') "id=" Text4 "name=" TEXT4 ">
--------------------------------------------------------------------------------
Verify illegal characters, SQL
function VDF ()
{
var i,j,name,value,message,length,type,a=vdf.arguments,cb_name;
for (i=0; i< (a.length-2); i+=3)
{
if (A[i].indexof (' # ')!=-1)
{
Name=fob (A[I].SUBSTR) (0,a[i].indexof (' # '));
Cb_name=fob (A[i].substr (A[i].indexof (' # ') +1), a[i].length));
}
Else
{
Name=fob (A[i]); Control Name
}
MESSAGE=A[I+1]; Hint Information
TYPE=A[I+2]; Type
if (type!= "R_time")
{
Value=name.value.replace (/+/g, ""); Control values
}
Else
{
Value=name.value;
}
}
--------------------------------------------------------------------------------
=============== Determines whether the check box selects ================//
if (type== "R_CB")
{
E=document.forms (0). elements;
var Flag=false;
for (i=0;i<e.length;i++)
{
if (e[i]!=cb_name)
{
if (e[i].checked==true)
{
Flag=true;
Break
}
}
if (i==e.length-1)
{
Break
}
}
if (Flag==false)
{
Alert (message+ "!/n"); The prompt that appears when you are empty
return false;
}
}
=============== Determines whether the dropdown box chooses ================//
if (type== "R_SL")
{
if (Name.selected==false)
{
Alert (message+ "!/n"); The prompt that appears when you are empty
return false;
}
}
=============== can not be null for the judgment ================//
if (type== "R")
{
if (value== "")//to determine if NULL
{
Alert (message+ "!/n"); The prompt that appears when you are empty
Name.focus ();
Name.select ();
return false;
}
}
=============== cannot be null for judgment, but does not get focus ================//
if (type== "O_r")
{
if (value== "")//to determine if NULL
{
Alert (message+ "!/n"); The prompt that appears when you are empty
return false;
}
}
=============== can only enter Chinese ================//
if (type== "R_china")
{
if (Value.search (/^[/u4e00-/u9fa5]+$/) ==-1)
{
Alert (message+ "!/n"); Judge cannot be empty
Name.focus ();
Name.select ();
return false;
}
}
=============== cannot be null, must be a number or character Judge ================//
if (type== "R_num_char")
{
if (value== "")
{
Alert (message+ "!/n"); The prompt that appears when you are empty
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9a-za-z]+$/) ==-1)
{
Alert (message+ "!/n"); The prompt that appears when you are empty
Name.focus ();
Name.select ();
return false;
}
}
=============== can be empty, not empty, fill in the number ================//
if (type== "num")
{
if (Value.search (/^[0-9]+$/) ==-1 && value!= "")
{
Alert (message+ "!/n"); Judge cannot be empty
Name.focus ();
Name.select ();
return false;
}
}
=============== cannot be empty, must be a digital ================//
if (type== "R_num")
{
if (value== "")
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9]+$/) ==-1)
{
Alert (message+ "!/n"); Judge cannot be empty
Name.focus ();
Name.select ();
return false;
}
}
=============== must enter a number less than n ================//
if (Type.indexof ("r_num<")!=-1)
{
Length=type.substring ((Type.indexof (' < ') +1), type.length); Get the number behind the rn<
if (value== "")//For empty tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9]+$/) ==-1)//Not a number of tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (value.length>length)//Limit number length limit
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
}
=============== must enter a number greater than n ================//
if (Type.indexof ("r_num>")!=-1)
{
Length=type.substring ((Type.indexof (' > ') +1), type.length); Get the number behind the rn<
if (value== "")//For empty tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9]+$/) ==-1)//Not a number of tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (value.length<length)//Limit number length limit
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
}
=============== must enter a number between a-b bits ================//
if (Type.indexof ("r_num#<>")!=-1)
{
Length=type.substr ((Type.indexof (' > ') +1), type.length);
Length=length.substr (0,length.lastindexof ("-"));
Length1=type.substring ((Type.indexof ('-') +1), type.length)//Get the number after rn<
if (value== "")//For empty tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9]+$/) ==-1)//Not a number of tips
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.length<length | | value.length>length1)//Limit number length limit
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
}
=============== cannot be empty, must be float type ================//
if (type== "R_float")
{
if (value== "")
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[0-9]+$/)!=-1 | | value.search (/^ ([0-9]+) | ( [0-9]+/. [0-9]*) | ([0-9]*/. [0-9]+) $/)!=-1)
{
return true;
}
Else
{
Alert (message+ "!/n"); Judge cannot be empty
Name.focus ();
Name.select ();
return false;
}
}
=============== to judge email, not necessarily input ================//
if (Type.indexof ("email")!=-1)
{
if (name.value!= "")
{
if (Value.search (/^[_/.a-z0-9]+@[a-z0-9]+[/.) [A-z0-9] {2,}$/i) ==-1)
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
}
}
=============== to judge email, must input ================//
if (Type.indexof ("R_email")!=-1)
{
if (name.value== "")
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
if (Value.search (/^[_/.a-z0-9]+@[a-z0-9]+[/.) [A-z0-9] {2,}$/i) ==-1)
{
Alert (message+ "!/n");
Name.focus ();
Name.select ();
return false;
}
}
=============== judgment date, such as 2000-12-20================//
if (type== "R_date")
{
Flag=true;
Getdate=value;
if (Getdate.search/^[0-9]{4}-(0[1-9]|[ 1-9]|1[0-2])-((0[1-9]|[ 1-9]) |1[0-9]|2[0-9]|3[0-1]) $/==-1)//Judging the input format is correct
{
Flag=false;
}
Else
{
var year=getdate.substr (0,getdate.indexof ('-'))//acquired year
The following action gets the month
var transition_month=getdate.substr (0,getdate.lastindexof ('-'));
var month=transition_month.substr (Transition_month.lastindexof ('-') +1,transition_month.length);
if (Month.indexof (' 0 ') ==0)
{
Month=month.substr (1,month.length);
}
The following action gets the date
var day=getdate.substr (Getdate.lastindexof ('-') +1,getdate.length);
if (Day.indexof (' 0 ') ==0)
{
Day=day.substr (1,day.length);
}
Alert (month);
Alert (Day)
return false;
if ((month==4 | | month==6 | | month==9 | | month==11) && (day>30))//4,6,9,11 Month Date cannot exceed 30
{
Flag=false;
}
if (month==2)//Judgment February
{
if (Leapyear (year))
{
if (day>29 | | day<1) {flag=false;}
}
Else
{
if (day>28 | | day<1) {flag=false;}
}
}
Else
{
Flag=true;
}
}
if (Flag==false)
{
Alert (message+ "!/n"); The prompt that appears when you are empty
Name.focus ();
Name.select ();
return false;
}
}
1. Length Limit
<script>
function test ()
{
if (DOCUMENT.A.B.VALUE.LENGTH>50)
{
Alert ("Cannot exceed 50 characters!") ");
Document.a.b.focus ();
return false;
}
}
</script>
<form name=a onsubmit= "return Test ()" >
<textarea name= "B" cols= "wrap=" "VIRTUAL" rows= "6" ></textarea>
<input type= "Submit" name= "Submit" value= "Check" >
</form>
2. Can only be Chinese characters
<input onkeyup= "value="/oblog/value.replace (/[^/u4e00-/u9fa5]/g, ') ">
3. "Can only be English
<script language=javascript>
function Onlyeng ()
{
if (!) ( EVENT.KEYCODE>=65&&EVENT.KEYCODE<=90))
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlyeng ();" >
4. Only the numbers
<script language=javascript>
function Onlynum ()
{
if (!) ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Consider the number keys on the keypad
Event.returnvalue=false;
}
</script>
<input onkeydown= "Onlynum ();" >
5. Only English characters and numbers
<input onkeyup= "value="/oblog/value.replace (/[/w]/g, "'") "onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ') ">
6. Verify the fuel tank format
<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. Shielding keywords (here shield * * * * and * * * *)
<script language= "javascript1.2" >
function Test () {
if ((A.b.value.indexof ("* *") = = 0) | | (A.b.value.indexof ("* * *") = = 0)) {
Alert (":)");
A.b.focus ();
return false;}
}
</script>
<form name=a onsubmit= "return Test ()" >
<input Type=text name=b>
<input type= "Submit" name= "Submit" value= "Check" >
</form>
8. Two times input password is the same
<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>
That's enough,:).
Shielding right button is cool
Oncontextmenu= ' return false ' ondragstart= ' return false ' onselectstart= ' return false '
Added to the body.
Two
2.1 Form items cannot be empty
<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.2 Compare the values of two table items
<script language= "JavaScript" >
<!--
function Checkform ()
if (Document.form.PWD.value!= document.form.PWD_Again.value) {
Alert ("The password you entered two times is different!") Please re-enter. ");
Document. ADDUser.PWD.focus ();
return false;
}
return true;
}
-->
</script>
2.3 Form items can only be numbers and "_" for phone/bank account verification, can be extended to domain name registration, etc.
<script language= "JavaScript" >
<!--
function Isnumber (String)
{
var letters = "1234567890-"; You can add your own input value
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 not legal!") ");
Document.form.TEL.focus ();
return false;
}
return true;
}
-->
</script>
2.4 Table Single input value/length limit
<script language= "JavaScript" >
<!--
function Checkform ()
{
if (Document.form.count.value > | | Document.form.count.value < 1)
{
Alert ("The input value cannot be less than 0 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>
2.5 Chinese/English/digital/email address legality judgment
<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 Ischinese (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)//numeric 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 ("Chinese name is illegal!") ");
Form.cnname.focus ();
return false;
}
if (! isnumber (Form). Publiczipcode.value)) {
Alert ("ZIP code not valid!) ");
Form. Publiczipcode.focus ();
return false;
}
return true;
}
-->
</SCRIPT>
2.6 Qualifying A form item cannot enter characters
<script language= "JavaScript" >
<!--
function contain (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 ("entered an illegal character");
Document.form.NAME.focus ();
return false;
}
return true;
}
-->
</script>
1. Check whether a string of strings is all composed of numbers
---------------------------------------
<script language= "Javascript" ><!--
function Checknum (str) {return Str.match (//d/) ==null}
Alert (Checknum ("1232142141"))
Alert (Checknum ("123214214A1"))
--></script>
2. How to judge whether it is a character
---------------------------------------
if (/[^/x00-/xff]/g.test (s)) alert ("contains Chinese characters");
else alert ("All characters");
3. How to judge whether to contain Chinese characters
---------------------------------------
if (Escape (str). IndexOf ("%u")!=-1) alert ("contains Chinese characters");
else alert ("All characters");
4. Mailbox Format Verification
---------------------------------------
Function Name: Chkemail
Function Introduction: Check whether for email address
Parameter description: The 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
---------------------------------------
Function Name: Fucchecknum
Feature Description: Check for numbers
Parameter description: The 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)
{
Indicates that a character is not a number
return 0;
}
}
Description is a number
return 1;
}
6. Telephone Number format verification
---------------------------------------
Function Name: Fucchecktel
Function Description: Check whether the phone number
Parameter description: The 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)
{
Indicates that there are characters that are not legal
return 0;
}
}
Description Legal
return 1;
}
7. A function to determine whether the input is Chinese
---------------------------------------
function Ischinese (s) {
var ret=true;
for (Var i=0;i<s.length;i++)
Ret=ret && (S.charcodeat (i) >=10000);
return ret;
}
8. A comprehensive function to judge the legality of user input
---------------------------------------
<script language= "JavaScript" >
Limit the number of digits of the input character to begin
M is user input, n is the number of digits to limit
function Issmall (m,n)
{
if ((m<n) && (m>0)
{
return (false);
}
Else
{return (true);}
}
9. Determine whether the password is entered in a consistent
---------------------------------------
function Issame (STR1,STR2)
{
if (STR1==STR2)
{return (true);}
Else
{return (false);}
}
10. Determine if the user name is a digital letter down line
---------------------------------------
function Notchinese (str) {
var reg=/[^a-za-z0-9_]/g
if (Reg.test (str)) {
return (false);
}else{
return (true); }
}
Universal check function for 2.8. form text Field
---------------------------------------
Function: Detects all input text that must be non-null, such as name, account number, email address, etc.
The checksum is now only for text fields, and if you want to target other domain objects in the form, you can change the criteria for judgment.
How to: Add title text to the text field you want to detect. The text is in the prompt message, you want to prompt the user with the Chinese name of the field. For example, to detect user names
HTML follows <input name= "txt_1" title= "name", and, of course, it's best to edit the domain with visual tools such as Dreamweaver or something.
If you want to detect numeric types of data, then the domain ID unified to SZ.
JavaScript to determine the date type is more cumbersome, so there is no date-type validation program. The master can be added.
Program to compare grass, just to provide a train of thought. Initiate :)
Oh, yes, function call method:< form onsubmit= "return Dovalidate ()" >
Copy Code code as follows:
function dovalidate ()
{
Fm=document.forms[0]//Detect only one form, if multiple can be changed Variable judgment conditions
for (i=0;i<fm.length;i++)
{
//detection criteria, which can be modified depending on type
if (fm[i].tagname.touppercase () = "INPUT" &A Mp;&fm[i].type.touppercase () = = "TEXT" && (fm[i].title!= ""))
if (fm[i].value= "/blog/=")//
{
S Tr_warn1=fm[i].title+ "cannot be empty!";
Alert (STR_WARN1);
Fm[i].focus ();
return false;
}
if (fm[i].id.touppercase () = "SZ")//Digital checksum
{
if (isNaN (fm[i].value))
{str_warn2=fm[i].title+ "format is not Yes ";
Alert (STR_WARN2);
Fm[i].focus ();
return false;
}
}
}
return true;
}