Hi, I created a javascript function to allow numbers from 0 to 30 and the characters A and D. I give a warning if it does not meet the criteria, but if the user clicks ok on the warning, the values ββstill remain in the input and can be updated in the database data. I want this user to not be able to enter anything into the input field, except for the characters A, D and numbers from 0 to 30, as is the case with type = number, we can only enter numbers. My javascript function: -
function validate() { var regex = /[ad0-9]/gi; var txt = document.getElementById('txt').value; var valid = true; var error = ''; if (regex.test(txt)) { if (!isNaN(txt)) { if (!(parseInt(txt) >= 0 && parseInt(txt) <= 30)) { valid = false; error = 'Please enter between 0 to 30.' } } } else { valid = false; error = 'Please enter between 0 to 30, A or D' } if (!valid) { alert(error); } }
javascript works fine with validation, but after clicking ok, the warning value still remains, and it also gives an error when the input field is empty to avoid this. Is there any other better way to create a function or can it be done using jquery. I'm new to jquery, if it's possible to do with jquery, that would be great. I would be very grateful if anyone could help.
source share