Home Web Front-end JS Tutorial JS form validation encyclopedia_form effects

JS form validation encyclopedia_form effects

May 16, 2016 pm 05:59 PM
form validation

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.lengthlength1) // Limit the length of the number
{
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?

"input2">


<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.

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;iret=ret && (s.charCodeAt(i)>=10000);
return ret;
}
8. Comprehensive function to judge the legality of user input
---------------------------------------

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use CodeIgniter4 framework in php? How to use CodeIgniter4 framework in php? May 31, 2023 pm 02:51 PM

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Laravel Development: How to validate form requests using Laravel Validation? Laravel Development: How to validate form requests using Laravel Validation? Jun 13, 2023 pm 01:34 PM

Laravel is a popular PHP web development framework that provides many convenient features to speed up the work of developers. Among them, LaravelValidation is a very practical function that can help us easily validate form requests and user-entered data. This article will introduce how to use LaravelValidation to validate form requests. What is LaravelValidationLaravelValidation is La

How to use Flask-WTF to implement form validation How to use Flask-WTF to implement form validation Aug 03, 2023 pm 06:53 PM

How to use Flask-WTF to implement form validation Flask-WTF is a Flask extension for handling web form validation. It provides a concise and flexible way to validate user-submitted data. This article will show you how to use the Flask-WTF extension to implement form validation. Install Flask-WTF To use Flask-WTF, you first need to install it. You can use the pip command to install: pipinstallFlask-WTF import the required modules in F

How to handle form validation using middleware in Laravel How to handle form validation using middleware in Laravel Nov 02, 2023 pm 03:57 PM

How to use middleware to handle form validation in Laravel, specific code examples are required Introduction: Form validation is a very common task in Laravel. In order to ensure the validity and security of the data entered by users, we usually verify the data submitted in the form. Laravel provides a convenient form validation function and also supports the use of middleware to handle form validation. This article will introduce in detail how to use middleware to handle form validation in Laravel and provide specific code examples.

How to implement form validation for web applications using Golang How to implement form validation for web applications using Golang Jun 24, 2023 am 09:08 AM

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

PHP form validation tips: How to use the filter_input function to verify user input PHP form validation tips: How to use the filter_input function to verify user input Aug 01, 2023 am 08:51 AM

PHP form validation tips: How to use the filter_input function to verify user input Introduction: When developing web applications, forms are an important tool for interacting with users. Correctly validating user input is one of the key steps to ensure data integrity and security. PHP provides the filter_input function, which can easily verify and filter user input. This article will introduce how to use the filter_input function to verify user input and provide relevant code examples. one,

Form validation and filtering methods in PHP? Form validation and filtering methods in PHP? Jun 29, 2023 pm 10:04 PM

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

How to use Hyperf framework for form validation How to use Hyperf framework for form validation Oct 20, 2023 pm 02:04 PM

How to use the Hyperf framework for form validation Introduction: With the development of web applications, form validation has become an important part of ensuring the accuracy and security of data. As a high-performance PHP development framework, the Hyperf framework provides powerful form validation functions. This article will introduce how to use the Hyperf framework for form validation and provide specific code examples. 1. Install the Hyperf framework: Use Composer to install: composercreate-proje

See all articles