Home > Web Front-end > JS Tutorial > JS form validation codes commonly used in work (including examples)_Form special effects

JS form validation codes commonly used in work (including examples)_Form special effects

WBOY
Release: 2016-05-16 18:16:28
Original
982 people have browsed it
Copy code The code is as follows:

////---------- -----Author Teng-------------
//Verify whether it is empty
function check_blank(obj, obj_name){
if(obj.value != ''){
return true;
}else{
alert(obj_name "Filled in cannot be empty!");
obj.value = "";
return false;
}
}

//Filter the length of input characters
function check_str_len(name,obj,maxLength){
obj.value=obj.value.replace(/(^s* )|(s*$)/g, "");
var newvalue = obj.value.replace(/[^x00-xff]/g, "**");
var length11 = newvalue. length;
if(length11>maxLength){
alert(name " cannot exceed " maxLength " characters!");
obj.value="";
obj.focus() ;
}
}

//Validation can only be numbers
function checkNumber(obj){
var reg = /^[0-9] $/;
if(obj.value!=""&&!reg.test(obj.value)){
alert('Only numbers can be entered!');
obj.value = "";
obj. focus();
return false;
}
}

//Verify the range of number size

function check_num_value(obj_name,obj,minvalue,maxvalue){
var reg = /^[0-9] $/;
if(obj.value!=""&&!reg.test(obj.value)){
alert(obj_name 'Only numbers can be entered ! ');
obj.value = "";
obj.focus();
return false;
}else if(minvalue>obj.value||obj.value>maxvalue){
alert(obj_name "range is " minvalue "-" maxvalue "!");
obj.value="";
obj.focus();
return false;
}

}

//Verification can only be letters and numbers
function checkZmOrNum(zmnum){
var zmnumReg=/^[0-9a-zA-Z]*$ /;
if(zmnum.value!=""&&!zmnumReg.test(zmnum.value)){
alert("Only letters or numbers can be entered, please re-enter");
zmnum .value="";
zmnum.focus();
return false;
}
}

//Verify double precision number
function check_double(obj,obj_name ){
var reg = /^[0-9] (.[0-9] )?$/;
if(obj.value!=""&&!reg.test(obj.value)) {
alert(obj_name 'Filled in must be a valid double precision number');
obj.value = "";
obj.focus();
return false;
}
}


//Select all checkboxes
function checkboxs_all(obj,cName){
var checkboxs = document.getElementsByName(cName);
for(var i =0;icheckboxs[i].checked = obj.checked;
}
}


//Verify zip code
function check_youbian(obj){
var reg=/^d{6}$/;
if(obj.value!=""&&!reg.test(obj.value)){
alert ('Postcode format input error! ');
obj.value = "";
obj.focus();
return false;
}
}

//Verify email format
function check_email(obj){
var reg = /^[a-zA-Z0-9_-] (.([a-zA-Z0-9_-]) )*@[a-zA-Z0-9_- ] [.][a-zA-Z0-9_-] ([.][a-zA-Z0-9_-] )*$/;
if(obj.value!=""&&!reg.test (obj.value)){
obj.select();
alert('E-mail format input error!');
obj.value = "";
obj.focus();
return false;
}
}

/*Verify landline number
0d{2,3} represents area code
[0 ]d{2,3} represents International area code
d{7,8} represents 7-8 digits (indicating phone number)
Correct format: area code-telephone number-extension number (write in full|write only phone number)
*/

function check_phone(obj){
var reg=/^(([0 ]d{2,3}-)?(0d{2,3})-)?(d{7,8 })(-(d{3,}))?$/;
if(obj.value!=""&&!reg.test(obj.value)){
alert('Phone number format input Error! ');
obj.value = "";
obj.focus();
return false;
}
}

//Verify mobile number ( Check mobile phone numbers starting with 13, 15, 18! )
function check_telephone(obj){
var reg= /^[1][358]d{9}$/;
if(obj.value !=""&&!reg.test(obj.value)){
alert('Mobile phone number format is incorrect!');
obj.value = "";
obj.focus();
return false;
}
}

//Verify whether it is Chinese
function isChinese(obj,obj_name){
var reg=/^[u0391-uFFE5] $/;
if(obj.value!=""&&!reg.test(obj.value)){
alert(obj_name 'Must enter Chinese! ');
obj.value = "";
obj.focus();
return false;
}
}

//Determine whether it is an IE browser

function checkIsIE(){
if(-[1,]){
alert("This is not IE!");
}else{
alert(" This is IE browser! ");
}
}

//Verify whether it is the correct URL
function check_IsUrl(obj){


}

//Check time size (compared with current time)
function checkDate(obj,obj_name){
var obj_value=obj.value.replace(/-/g,"/");/ /Replace characters and change to standard format (test format is: '2009-12-10')
// var obj_value=obj.value.replace("-","/");//Replace characters and change into standard format (test format is: '2010-12-10 11:12')
var date1=new Date(Date.parse(obj_value));
var date2=new Date();//Get Today's date
if(date1>date2){
alert(obj_name "Cannot be greater than the current time!");
return false;
}
}
/// Copyright © 2010-2012 com.zljy.teng.js////
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template