javascript - How to validate a currency input -
i creating form, , includes salary, need check if input value salary valid currency , has 2 decimal places because database accepts. if input salary has alphabets (a-z) or symbols (!@#%^&*() except $ or other currency sign) should change border color.
example:
10000.00 25846.00 213464.12
code:
function isnumeric(){ var numeric = document.getelementbyid("txtsalary").value; var regex = /^\d+(?:\.\d{0,2})$/; if (regex.test(numeric)){ $("#txtsalary").css("border-color","#ffffff"); }else{ $("#txtsalary").css("border-color","#ff0000"); } }
this working wanted too, problem got 6 more input boxes needs kind of validation. how can make function, when called change border-color of specific selector , return false value not numeric like:
isnumeric(selector); function isnumeric(selector){ var regex = /^\d+(?:\.\d{0,2})$/; $(selector).filter(function() { return (regex.test(this.value)); }).css("border-color","#ff0000"); }
thanks!
try use html5 pattern directly on input:
<input type='text' pattern='^\d+(?:\.\d{0,2})$'/>
it works modern browsers. make border red if pattern false
Comments
Post a Comment