search
HomeWeb Front-endHTML TutorialHTML forms and validation events

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
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
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)