Regular check of HTML forms-automatically replaces strings that do not conform to a specified regular expression with an empty one

Source: Internet
Author: User

1 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">2 <HTML>3   <Head>4     <title>Checknum.html</title>5     <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">6     7     <Scripttype= "Text/javascript">8         functionChecknum (EL) {9             varReg= /^-?\d+ (\.\d+)? $/;//Defining Regular ExpressionsTen             if(El.value.search (REG)==-1){//Search () method: If the user enters a value that does not conform to the regular expression, it returns-1 One El.value="";//empty if non-conforming regular expression A             } -         } -     </Script> the   </Head> -   <Body> -Please enter a number:<inputtype= "text"onchange= "Checknum (this)"/> -   </Body> + </HTML>

The above regular expression ^-?\d+ (\.\d+)? $ means that the user input is not a real number (including integers and decimals), which means the user can only enter a minus sign (-), The number 0-9 and the decimal point (.), and the minus sign can only appear at the beginning of the number, cannot appear in the middle or end of the number, the decimal point can only appear in the middle of the number once. Any value entered that does not satisfy the condition will be automatically emptied.

Regular check of HTML forms-automatically replaces strings that do not conform to a specified regular expression with an empty one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.