I'm using jQuery validation on drop-down field, on based of selected option I have to validate Next input feild, i.e. if I have selected pan card then in next field alphanumeric characters allowed. For that I'm using Regular Expression. And then on select of Adhar card only Numeric values allowed in next input field. I want to validate it once user clicks on drop down option. I tried, but code is not working. Please check my code where it goes wrong.
$('#idproof').change(function() {
var selection = $(this).val();
if (selection == 'pan') {
var val = document.registration.idnumber;
var numbers = /^[0-9]+$/;
if (val.value.match(numbers)) {
document.registration.zip.focus();
return true;
} else {
$('#error').empty();
$('#error').append('pan number must have numeric characters only');
val.focus();
return false;
}
} else if (selection == 'Adhar') {
var adhar = document.registration.idnumber;
var letters = /^[0-9a-zA-Z]+$/;
if (val.value.match(letters)) {
document.registration.zip.focus();
return true;
} else {
$('#error').empty();
$('#error').append('Adhar number must have alphanumeric characters only');
val.focus();
return false;
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<div id="error"></div>
<form id="reg" name="registration">
<ul>
<li>IdProof:
<select id="idproof">
<option value="Default">Please select a id type</option>
<option value="pan">pancard</option>
<option value="Adhar">AdharCard</option>
</select>
</li>
<li>Id Number:
<input type="text" id="idnumber" />
</li>
</ul>
</form>