Currently, I am in the process of developing a web form that includes a text field intended to receive numeric values. If the user enters non-numeric characters into this field, the form will not submit. However, there is no error message displayed to notify the user of their mistake.
My query revolves around finding a way to dynamically check the input content and provide immediate feedback to the user if it is incorrect. Ideally, I would like the border of the input field to turn green when the entry is valid, and red if invalid, accompanied by a message prompting the user to only enter numbers.
Below is a snippet of what I have implemented so far:
HTML:
<input type="text" class="btn-blue" id="testing"></input>
JS:
$('#testing').attr('placeholder', 'Enter Amount');
var useramount = $("#testing").val();
if (useramount.match(/^\d+$/)) {
$("#testing").css({border: "2px solid #33CC00 !important"});
}
else {
$("#testing").css({border: "2px solid #FF0000 !important"});
$("#testing").innerHTML = "";
$('#testing').attr('placeholder', 'Only Numbers Please');
}
This validation technique was inspired by a similar question on Stack Overflow: Check If only numeric values were entered in input. (jQuery)
I welcome any assistance or guidance on optimizing this functionality.