Home > Web Front-end > JS Tutorial > Implementation code for setting page text box based on jquery that can only input numbers_jquery

Implementation code for setting page text box based on jquery that can only input numbers_jquery

WBOY
Release: 2016-05-16 18:07:37
Original
966 people have browsed it

The code is as follows:

Copy code The code is as follows:

$("#money").bind ("propertychange",function() {
if(""!=this.value){
var str = this.value.replace(/(^s*)|(s*$)/g, "");
if(this.value != str )
this.value = str;
}
if( isNaN(Number(this.value)))
this.value = this.value.replace(/[D]/,'');
});

JQuery is used here to bind to the onpropertychange event of the text box with the id of money.
The code below even blocks the decimal point
Copy the code The code is as follows:

$("#phone").bind("propertychange", function() {
if(""!=this.value){
var str = this.value.replace(/(^s*) |(s*$)/g, "");
if(this.value != str )
this.value = str;
}
if (this.value.indexOf(' .') != -1) {
this.value = this.value.replace(/[.]/, '');
this.focus(); }
if (isNaN(Number (this.value))) {
this.value = ($.trim(this.value)).replace(/[D]/, '');
this.focus(); } }) ;

Finally, it is best to block the input method. This can be achieved through css, ime-mode:disabled.
If it is very strict, you can add a prohibition on pasting and dragging.
How to prohibit pasting and dragging
Prohibit dragging and pasting in the text box

Implement the function of prohibiting dragging and pasting in the text box in css

Create a Css as follows:
Copy the code The code is as follows:

.TextBox_NotDragpaste

{
ondragenter:expression(ondragenter=function(){return false;});
onpaste:expression(onpaste=function(){return false;});
}

If you still need to disable the function of inputting Chinese, you only need to add one more sentence.

is as follows:
Copy code The code is as follows:

.TextBox_NotDragpaste

{
ime-mode:disabled;
ondragenter:expression(ondragenter=function(){return false;});
onpaste:expression(onpaste=function(){return false;} );
}
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template