There is a textbox in the form, in which only specific number inputs are allowed. i.e
0.5, 1, 1.5, 2, 2.5, 3........ and so on
How can i mask this textbox for these inputs? Or what regex should i use?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you only want to validate on submit, you can simply use
parseFloat,Math.floorandtoString:Or you can perform a regex check with string replace,
.replace(/[^0-9\.]/g, ""). That only allows numbers and periods/decimal points. Unfortunately, it does not prevent multiple decimal points.If you want to restrict actual keystrokes, you need something more. Attach the following function to the
keyupevent for your textbox. Remember to usereturn isValidKey(this, event)instead of simplyisValidKey(this, event):
http://jsfiddle.net/SHJsz/
Update: It is now slightly more strict, but still not foolproof. You can still have a number with a leading 0 or start with a decimal point if the user uses backspace or delete to remove the original starting numbers.
http://jsfiddle.net/SHJsz/1/