简体   繁体   中英

regular expression for not allow minus and decimal numbers and zero in javascript

I need regular expression for nopt allowing minus and decimal numbers and zero using javascript. I have code but it is not working. help anyone.

$("#hinizio").keyup(function () {
var match = timeRegex.test($(this).val());
alert(match ? 'matches' : 'does not match');
}); 

http://jsfiddle.net/9CVCC/45/

Allowing Alphanumeric Characters (But Not 0)

If you wanted to allow for any number alphanumeric characters without minus symbols - , decimal points . or just zero as the string, you could use :

/^(?!0)[\da-zA-Z]+$/i

This will explicitly allow one or more alphanumeric characters, however it will not accept 0 explicitly. You can see an example of it here .

Allowing Only Numeric Characters (But Not 0)

If you wanted to only allow numbers but not 0, you could use :

/^(?!0)\d+$/i

You can see an example of this option here .

Sure!

document.getElementById(target).value = newVal.replace(/[^0-9]/g, "");

The above will remove everything except normal numbers, if you want to keep any special characters, add them to the expression.

Best of all its raw JS, just replace the selector with your current working value from this.

$(this).val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM