I have a textbox which allows users to enter their name and it should be like LastName,First Name format. The below JS works perfectly as expected. I will call this function on OnClientClick
event in the button click. But I have n number of textbox that needs to be validated and now I would need to replicate this function for all the textboxed repeatedly. Is there any way to reconstruct this that can be used for all the textbox which will reduce the code quantity and pain in the neck.
function onFirstNameLastName() {
var name = /^\w{1,20}\, \w{1,20}$/
var Id = document.getElementById("bodycontent_Updates");
var flabels = document.getElementById('<%=((TextBox)Updates.FooterRow.FindControl("txtName")).ClientID %>');
if (flabels != null) {
if (flabels.value != "") {
if (!name.test(flabels.value)) {
alert('Please enter Last Name, First Name format');
document.getElementById('<%=((TextBox)Updates.FooterRow.FindControl("txtName")).ClientID %>').focus();
return false;
}
else {
return true;
}
}
else if (name.test(flabels.value)) {
return true;
}
else if (flabels.value == "") {
return true;
}
}
}
The current regular expression I use will allow name like this Clark, Michael
but I also need to allow names in this format too Tim Clark, Duncan Vince
which my current regex doesn't support.
Really appreciate any suggestions.