0

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.

Michael
  • 305
  • 1
  • 5
  • 19
  • 2
    Instead of searching for a textbox with ID could you search for all textboxes of class "Firstname"? – Liath Jan 05 '15 at 10:44
  • @Liath - Could you pls brief and sorry for not catching it up. – Michael Jan 05 '15 at 10:46
  • 1
    possible duplicate of [Javascript validation for multiple textboxes](http://stackoverflow.com/questions/8747328/javascript-validation-for-multiple-textboxes) – Paul Zahra Jan 05 '15 at 10:51
  • I've got got a full solution but something along the lines of foreach(textbox in getElementsByTagName("input")) {onFirstNameLastName(textbox)}; however I tend to use jQuery so don't have much experience in this approach. http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript may help to select textboxes of class – Liath Jan 05 '15 at 10:52
  • @Liath - I have got a solution, but I have got a problem now. 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. – Michael Jan 06 '15 at 12:35
  • @Michael Great news! I would suggest posting your solution as the answer to this question then opening a new question with the regex you've got and examples of which names do and don't match. One question per question ;-) – Liath Jan 06 '15 at 14:29
  • @Liath - Hey, I have a new post and pls take a look http://stackoverflow.com/questions/27816630/regular-expression-for-lastname-firstname-with-space – Michael Jan 07 '15 at 09:52

1 Answers1

0

If your all textboxes contains same data then first use ValidatorHookupControl http://msdn.microsoft.com/en-us/library/aa338815(v=vs.71).aspx to register your controls for validation and attach same JS function name which will validate data in it.

If you have different data in your textboxes then use custom validator and trigger the same on Submit click. In custom validator go through each text box and call required validation function.

Amit
  • 882
  • 6
  • 13