-1

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?

Expressingx
  • 1,480
  • 1
  • 14
  • 39

3 Answers3

3

Add the end of string anchor ($):

^[0-9]+$
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
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