Reputation: 4727
I want to set focus each time on the textbox if alert message is prompted. How should I deal where I use that javascript function for multiple times. Here is my js code
function validateLandline(landfield) {
var reg = /\d{5}([- ]*)\d{6}/;
if (reg.test(landfield.value) == false) {
jAlert('Kindly enter valid landline no', 'INFORMATION');
return false;
landfield.focus();
}
return true;
}
and textbox html
<input type="text" id="txtStoreSiteL1" onchange="validateLandline(this);" maxlength="20" />
Upvotes: 0
Views: 918
Reputation: 8249
Based on the comments, I have created a small snippet below:
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test($email);
}
$('input').on('input focusout', function() {
$(this).removeClass('error');
if (!validateEmail($(this).val())) {
$(this).addClass('error').focus();
}
});
.error {
border: 2px solid #d00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtStoreSiteL1" maxlength="20" />
<input type="text" id="txtStoreSiteL2" maxlength="20" />
Upvotes: 1