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