I have a problem trying to check if a text field contains only az values 0-9 using JavaScript.
I have a text box on my page:
<input type="text" id="first_name" class="first_name" value="">
I use the following JavaScript function to validate my text field:
// Function to check letters and numbers function alphanumeric(inputtxt) { //alert(inputtxt); var letters = "/^[0-9a-zA-Z]+$/"; if(inputtxt.value.match(letters)) { alert('accepted'); } else { alert('Please input alphanumeric characters only'); return false; } }
And I call my function as follows:
var test = alphanumeric(document.getElementById("first_name").value);
However, nothing happens.
If I warn 'inputtxt' in its alphanumeric function, it returns my value, which I have in my text box, so I know that there is a value that needs to be checked, but it does not seem to continue from here.
Does anyone know where I was wrong?
I am trying to do this using JavaScript (without jQuery).
source share