Table of Contents
Commonly used regular expressions:
Home Web Front-end HTML Tutorial HTML forms and validation events

HTML forms and validation events

Sep 12, 2016 pm 05:27 PM

1. Form verification

(1). Non-empty verification (remove spaces)

(2). Comparison verification (compare with a value)

(3). Range verification (judgment based on a range)

(4). Fixed format verification: verification of phone number, ID number, email, credit card number, etc.; regular expressions are required for verification.

(5).Other verification

2, regular expression

Use symbols to describe writing rules:/ Write regular expressions in the middle /

^: matches the beginning, $: matches the end; /^ve/begins with ve /ve$/ends with ve

d: an arbitrary number

w: an arbitrary number or letter

s: an arbitrary string

{n}: Repeat the expression on the left n times

{m,n}: Repeat the expression on the left at least m times, and at most n times
{m, }: Repeat the expression on the left at least m times, and at most no limit

+: The expression on the left appears at least once and at most unlimited, equivalent to {1,}

*: The expression on the left appears at least 0 times and at most no limit, equivalent to {0,}

?: The expression on the left appears at least 0 times and at most 1 time, which is equivalent to {0, 1}

[a,b,c]: can only take one of the contents in square brackets

[a-z] or [1-9]: choose one in the range

|: represents or; (): priority; : escaping--"( )" is the parentheses to appear and needs to be escaped

3, event

An event has three elements: event source, event data, and event handler

You can add return false; to prevent the default operation

onclick: Triggered by mouse click

ondblclick: Double click trigger

onmouseover: Triggered when the mouse moves above

onmouseout: Triggered when the mouse leaves

onmousemove: Triggered when the mouse moves over it

onchange: Triggered whenever the content changes

onblur: triggered when focus is lost

onfocus: Triggered when focus is obtained

onkeydown: Triggered when the key is pressed

onkeyup: Triggered when the button is lifted

onkeypress: event occurs when the user presses and releases any alphanumeric key. But system buttons (e.g. arrow keys, function keys) are not recognized.

Example: Verify email based on regular expression

function checkemail() { var v4 = trim(u4.value); var reg = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+) *$/; if(v4.match(reg) != null) { imgs4.setAttribute("src","imges/1.png"); return true; } else imgs4.setAttribute("src","imges /2.png"); return false; } }

Regular expression supplement:

How to create regular expressions:

var patten= new RegExp(/^[0-9]{17}[0-9|X]$/);/*RegExp()The formulas in the brackets need to be defined by yourself:

1[]There is only one element in it

2, ()You can write a word or formula inside 3, {} which represents the quantity
4, ^: Start with a certain element and write it in Before the element
5, $: ends with a certain element, written after the element */

Example:

1, Regular expression verification ID card:

ID card:

/*javascriptPart*/

var a= document.getElementById("1").value; var pattern= new RegExp(/^[0-9]{17}[0-9|X]$/);

if(patten.test(a))

{ alert("The input is correct"); }

else

{ alert("Input error"); }

2, Regular expression verification email:

Email: Submit" onclick="mail()" />

function mail()

{

var pattern2= new RegExp(/^[0-9|A-z|_]{1,17}[@][0-9|A-z]{1,3}.(com)$/)

var mail = document.getElementById("2").value;

if(patten2.test(mail))

{         alert(" entered correctly");      }

else

{ alert("Input error"); }

}

Commonly used regular expressions:

号 Matching domestic phone number:

d {3} -d {8} | d {4} -d {7} Comments: matching forms such as 0511-4405222
or 021-8788822 match Tencent QQ
number: [1-9][0-9]{4,} Comment: Tencent QQ
number starts from 10000 Matches Chinese postal code: [1- 9]d{5}(?!d)
Comment: China’s postal code is 6
digits Matching ID card: d{15}|d{18}
Comment: China’s ID card is 15
bits or 18bits Match ip
address: d+.d+.d+.d+ Comment: Useful when extracting ip
address Match specific numbers:
^[1-9]d*$                                                                                  Integer ^[1-9]d*|0$ //
matches non-negative integers (positive integers+ 0
)
^-[1-9]d*|0$ //matches non-negative integers Positive integer (negative integer
+ 0) ^[1-9]d*.d*|0.d*[1-9]d*$ //Match positive floating point number
^-( [1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point numbers ^-?([1-9]d*.d*|0. d*[1-9]d*|0?.0+|0)$ //Match floating point numbers ^[1-9]d*.d*|0.d*[1-9]d* |0?.0+|0$ //
Match non-negative floating point numbers (positive floating point numbers+ 0
)
^(-([1-9]d*.d*|0.d*[ 1-9]d*))|0?.0+|0$ //Match non-positive floating point numbers (negative floating point numbers
+ 0) Comments: Useful when processing large amounts of data, specific applications Note the correction
Match specific strings: ^[A-Za-z]+$ // Match a string consisting of 26
English letters
^[A-Z]+$ // Matches a string consisting of
26 uppercase letters of English letters ^[a-z]+$^[A-Za-z0-9]+$ //matches a string consisting of numbers and 26 English letters
^w+$ //matches a string consisting of numbers, 26 A string consisting of letters or underscores
The verification function and its verification expression when using the RegularExpressionValidatorvalidation control are introduced as follows:
Only numbers can be entered: "^[0-9]*$"
        Only ndigits can be entered: “^d{n}$”  
    Only at least ndigits can be entered: “^d{n,}$”  
  Only Numbers that can be entered with m-n digits: “^d{m,n}$”
Only numbers starting with zero and non-zero can be entered: “^(0|[1-9][0-9 ]*)$”
You can only enter positive real numbers with two decimal places: “^[0-9]+(.[0-9]{2})?$”
You can only enter 1- Positive real numbers with 3 decimal places: “^[0-9]+(.[0-9]{1,3})?$”
Only non-zero positive integers can be entered: “^+ ?[1-9][0-9]*$”
Only non-zero negative integers can be entered: “^-[1-9][0-9]*$”
Only the length of can be entered 3 characters: "^.{3}$"
You can only enter a string consisting of 26 English letters: "^[A-Za-z]+$"
You can only enter a string consisting of 26 uppercase English letters: “^[A-Z]+$”
You can only enter a string consisting of 26 lowercase English letters: “^[ a-z]+$”
You can only enter a string consisting of numbers and 26 English letters: “^[A-Za-z0-9]+$”
You can only enter a string consisting of numbers, A string of 26 English letters or underscores: “^w+$”
Verify user password: “^[a-zA-Z]w{5,17}$”The correct format is: It starts with a letter and has a length between 6-18.
can only contain characters, numbers and underscores.
Verify whether it contains characters such as ^%&'',;=?$": "[^%&'',;=?$x22]+"
Only Chinese characters can be entered: " ^[u4e00-u9fa5],{0,}$”
VerificationEmailAddress: “^w+[-+.]w+)*@w+([-.]w+)*.w+([-. ]w+)*$”
VerificationInternetURL: “^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$”
Verification phone number Number: “^((d{3,4})|d{3,4}-)?d{7,8}$”
  The correct format is: “XXXX-XXXXXXX”, “XXXX -XXXXXXXX","XXX-XXXXXXX", "XXX-XXXXXXXX"
, "XXXXXXX", "XXXXXXXX".
Verify ID number (15digits or 18digits): “^d{15}|d{}18$”
Verify 12months of one year: "^(0?[1-9]|1[0-2])$"The correct format is: "01"-"09" and "1" "12"
Verify the 31day of a month: “^((0?[1-9])|((1|2)[0-9])|30|31)$”
The correct format is : "01" "09" and "1" "31" .
Regular expression matching Chinese characters: [u4e00-u9fa5]
Matches double-byte characters (including Chinese characters ) : [^x00-xff] matches empty Regular expression for line:
n[s| ]*r Regular expression for matching
HTML tag: /<(.*)>.*|<(.*) />/ Regular expression matching leading and trailing spaces:
(^s*)|(s*$) Regular expression matching
Email address: w+([-+.]w+)*@w+( [-.]w+)*.w+([-.]w+)* Regular expression matching URL
URL: http://([w-]+.)+[w-]+( /[w- ./?%&=]*)?

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

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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles