I have input field which db is expecting to come as integer. if i type 60 it works fine, but 60.00 doesn't. I did RegExp for validation with the ^[0-9]+/ expession. It works fine for inputs like 60.asdass, 60.0320320, dasdasdas.60 etc. but if i type 60. and it evaluates to true and it passes the validation and i get an error from db. How can i make my regex to sets as false in this situation?
Asked
Active
Viewed 37 times
-1
Expressingx
- 1,427
- 1
- 13
- 33
3 Answers
3
Add the end of string anchor ($):
^[0-9]+$
Dmitry Egorov
- 9,337
- 3
- 23
- 39
-
Thanks! Ill accept it in 10 mins. – Expressingx Jun 08 '17 at 14:52
0
You can use this. By using anchors it will fail if the full input is not the regular expression specified.
^\d+$
Eduardo Dennis
- 13,103
- 13
- 78
- 104
0
You could test the rest of the array as well and use start and end of the string as well for testing.
function check() {
var element =document.getElementById('input'),
value = element.value;
element.style.color = /^\d+(\.\d+)*$/.test(value) ? '#000000': '#ff0000';
}
<input id="input" type="text" onkeyup="check()">
Nina Scholz
- 351,820
- 24
- 303
- 358