jquery Positive Integer Digit Check Regular Expression
- 2021-07-10 18:13:18
- OfStack
Restricting what users enter can be done as follows:
$("#id").keyup(fucntion(){// Verify and replace the contents of the current action box immediately after the keyboard is pressed
var value = $(this).val();
$(this).val(value.replace(reg,replace_data));
});
The most common one is to restrict users to entering only numbers
var reg = new RegExp("[^0-9]","g");
[^ 0-9] denotes a number other than 0-9, g denotes a global match, and i denotes a case mismatch
Note: [^ x] matches any character except x, and [^ aeiou] matches any character except aeiou
The other method is exhaustive method
var reg = new RegExp("[a-zA-Z\u4e00-\u9fa5,.!?(),。..;;?、]","ig");
In this way, the contents of RegExp should list as many characters as possible that you don't want users to enter, where\ u4e00-\ u9fa5 represents Chinese characters and ig represents case-insensitive global matching
When you enter a number, you don't want the user to enter a number of "01", "001", etc., you can do the following:
if(rate.length > 1){
var reg = new RegExp("^[0]*","g");
var num = rate.replace(reg,"");
$(this).val(num);
}
^ [0] * means starting with 0, ^ is the starter, and * means repeating zero or more times
Note: I always feel that this method is not the best, but I can only think of doing it for the time being. If there is a better way, welcome to share, thank you