Home > Article > Web Front-end > JS form validation encyclopedia_form effects
1. Only numbers and English can be entered:
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 full-width input is possible:
4. Only Chinese characters can be input:
--------------------------------------------- ----------------------------------------
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]; // Prompt message
type=a[i 2]; // Type
if (type!="r_time")
{
value=name.value.replace(/ /g, " "); // Control value
}
else
{
value=name.value;
}
}
---------- -------------------------------------------------- ------------------
// =============== Determine whether the check box is selected====== =========== //
if (type=="r_cb")
{
e=document.forms(0).elements;
var flag=false ;
for (i=0;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 it is empty
return false;
}
}
// =============== Determine whether the drop-down box is selected ================ / /
if (type=="r_sl")
{
if (name.selected==false)
{
alert(message "!/n"); //Empty The prompt that appears when
return false;
}
}
// =============== Judgment that it cannot be empty ======= ========= //
if (type=="r")
{
if (value=="") // Determine whether it is empty
{
alert(message "!/n"); //The prompt that appears when it is empty
name.focus();
name.select();
return false;
}
}
// =============== Cannot be empty, but does not get focus ================ //
if (type=="o_r")
{
if (value=="") // Determine whether it is empty
{
alert(message "!/n"); / / Prompt that appears when it is empty
return false;
}
}
// =============== Only Chinese can be entered ===== =========== //
if (type=="r_china")
{
if (value.search(/^[/u4e00-/u9fa5] $/) ==-1)
{
alert(message "!/n"); // The judgment cannot be empty
name.focus();
name.select();
return false;
}
}
// =============== Cannot be empty, must be a number or character. ========== ====== //
if (type=="r_num_char")
{
if (value=="")
{
alert(message "!/n" ); //The prompt that appears when it is empty
name.focus();
name.select();
return false;
}
if (value.search(/^[ 0-9a-zA-Z] $/)==-1)
{
alert(message "!/n"); //The prompt that appears when it is empty
name.focus();
name.select();
return false;
}
}
// =============== Can be empty, not empty When, fill in the number ================ //
if (type=="num")
{
if (value.search(/^[ 0-9] $/)==-1 && value!="")
{
alert(message "!/n"); // The judgment cannot be empty
name.focus();
name.select();
return false;
}
}
// =============== cannot be empty, must be a number Judgment ================ //
if (type=="r_num")
{
if (value=="")
{
alert(message "!/n");
name.focus();
name.select();
return false;
}
if (value.search( /^[0-9] $/)==-1)
{
alert(message "!/n"); // The judgment 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 after rn<
if (value=="") // Prompt for empty
{
alert(message "!/n");
name.focus();
name.select();
return false;
}
if (value.search(/^[0-9 ] $/)==-1) // Not made of numbers
{
alert(message "!/n");
name.focus();
name.select() ;
return false;
}
if (value.length>length) // Limit the length of the number
{
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 back of rn< Number
if (value=="") // Prompt for empty
{
alert(message "!/n");
name.focus();
name .select();
return false;
}
if (value.search(/^[0-9] $/)==-1) // Not made of numbers
{
alert(message "!/n");
name.focus();
name.select();
return false;
}
if (value.length< length) // Limit the length of numbers
{
alert(message "!/n");
name.focus();
name.select();
return false ;
}
}
// ===============Must enter a number between a-b ============= === //
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=="") // Prompt for empty
{
alert(message "!/n");
name.focus();
name.select();
return false;
}
if (value.search(/^[0-9] $/)==-1) // Not a reminder made of numbers
{
alert(message "!/n");
name.focus();
name.select();
return false;
}
if (value.length
{
alert(message "!/n");
name. focus();
name.select();
return false;
}
}
// =============== cannot be empty , must be of 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"); // The judgment cannot be empty
name.focus();
name.select();
return false;
}
}
/ / =============== To determine the email, you do not have to enter ================ //
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 determine email, be sure to enter================ //
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;
}
}
// =============== Determine the 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) // Correct when judging the input format
{
flag=false;
}
else
{
var year=getdate.substr(0,getdate.indexOf('-')) // Get the year
// The following operation is to get 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 operation is to obtain 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)) // The dates in 4, 6, 9 and 11 months cannot exceed 30
{
flag=false;
}
if (month= =2) // Determine 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 it is empty
name.focus ();
name.select();
return false;
}
}
1. Length limit
<script> <br>function test() <br> { <br>if(document.a.b.value.length>50) <br>{ <br>alert("Cannot exceed 50 characters!"); <br>document.a.b.focus(); <br>return false; <br>} <br>} <br></script>
2. Can only be Chinese characters
3." It can only be in English
4. Can only be numbers
7. Block keywords (*** and **** are blocked here)
8. Twice Are the input passwords the same?
<script> <br>function check () <br>{ <br>with(document.all){ <br>if(input1.value!=input2.value) <br>{ <br>alert("false") <br>input1.value = ""; <br>input2.value = ""; <br>} <br>else document.forms[0].submit(); <br>} <br>} <br></script>
That’s enough :)
It’s cool to block the right click
oncontextmenu="return false" ondragstart="return false" onselectstart="return false"
Add to body
Two
2.1 Table Single item cannot be empty
2.2 Compare whether the values of two form items are the same
2.3 Form items can only be numbers and "_", which are used for phone/bank account verification and can be extended to domain name registration, etc.
2.4 Form item input value/length limit
2.5 Chinese/English/numeric/email address validity judgment
2.6 Limit the characters that cannot be entered in form items
1. Check whether a string consists entirely of numbers
---------------------------------- ----
2. How to determine whether it is a character
---- ----------------------------------
if (/[^/x00-/xff]/ g.test(s)) alert("Contains Chinese characters");
else alert("All characters");
3. How to judge whether it contains Chinese characters
--------- -------------------------------
if (escape(str).indexOf("%u")!=- 1) alert("contains Chinese characters");
else alert("all characters");
4. Email format verification
---------------- -----------------------
//Function name: chkemail
//Function introduction: Check whether it is an Email Address
// Parameter description: String to be checked
//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. Number format verification
---------------------------------------
//Function name: fucCheckNUM
//Function introduction: Check whether it is a number
//Parameter description: The number to be checked
//Return value: 1 means it is a number, 0 means it is not a number
function fucCheckNUM(NUM)
{
var i,j,strTemp;
strTemp="0123456789";
if ( NUM.length== 0)
return 0
for (i=0;i
j=strTemp.indexOf(NUM.charAt(i));
if (j==-1)
{
//Indicates that there are characters that are not Number
return 0;
}
}
//The description is number
return 1;
}
6. Phone number format verification
----- ----------------------------------
//Function name: fucCheckTEL
//Function introduction : Check whether it is a phone number
//Parameter description: The string to be checked
//Return value: 1 is legal, 0 is illegal
function fucCheckTEL(TEL)
{
var i,j,strTemp;
strTemp="0123456789-()# ";
for (i=0;i
j=strTemp.indexOf (TEL.charAt(i));
if (j==-1)
{
//Indicates that some characters are illegal
return 0;
}
}
//Explanation is legal
return 1;
}
7. Function to determine whether the input is Chinese
-------------------------- ------------------
function ischinese(s){
var ret=true;
for(var i=0;i
return ret;
}
8. Comprehensive function to judge the legality of user input
---------------------------------------