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 45 times
-1
Expressingx
- 1,480
- 1
- 14
- 39
3 Answers
3
Add the end of string anchor ($):
^[0-9]+$
Dmitry Egorov
- 9,542
- 3
- 22
- 40
-
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+$
Eddie Martinez
- 13,582
- 13
- 81
- 106
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
- 376,160
- 25
- 347
- 392