The content of the js verification input box can only contain numbers and numbers with decimal points. Although it is a bit strange on the Internet, it is concise. According to the author's verification, there is no problem in various browsers! The following code is shared as follows:
The first method is to enter only a number and a number with a decimal point. This is correct in IE but not compatible with firefox.
<input type="text" name="price" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" />
The second method is effective in IE and Firefox as follows:
<Input type = "text" name = "price" onchange = "if (/\ D /. test (this. value) {alert ('only numbers allowed '); this. value = '';}"/>
The third method has no problem in IE and Firefox:
<input type="text" name="price" value="" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}" />
Articles you may be interested in
- Javascript restricts that only numbers (including decimal places) can be entered in the text box)
- Js restrictions: Only English letters and numbers can be entered. Chinese characters and other special characters cannot be entered.
- Js obtains the number of rows and columns of a table (compatible with IE and firefox)
- Javascript determines whether the mouse is left or right-click-compatible with ie, firefox, chrome, and other major browsers
- Js obtains the height of the scroll bar from the top and bottom of the browser. It is compatible with ie and firefox.
- Js shields mouse and keyboard events (including right-click, direction key, backspace key, F5 refresh key, etc.), compatible with IE and firefox
- Javascript shield right-click, compatible with IE and firefox
- Js shield direction keys, compatible with IE and firefox