ASP.Net button call javascript function

I have a javascript function that looks at input on a SSN and then corrects it for the proper dashes. I origincally used this in a traditional ASP page, and it worked fine. Now I want to use it in an ASP.Net page. The user enters a SSN and clicks a button to find the record. It returns the correct data for that number. The numbers are stored in the database with the XXX-XX-XXXX format. So when a user enters one without the dashes, this javascript cleans it up when the user clicks search. How can I get it to run in ASP.Net? I have the javascript in the page, but I'm not sure how to call it properly with the button. The On_Click event runs the search. Do I use it in the OnClientClick event? The button itself is simple:
<asp:Button ID="Find" runat="server" Text="Find User" onClick="Find_Click" onClientClick="form_Validation()"></asp:Button>

The javascript is here:

<script type="text/javascript">
  var reSSN = /^[0-9]{3}\-[0-9]{2}\-[0-9]{4}$/;
  var altSSN = /^\d{9}/;
  var altPattern = /(\d{3}) (\d{2}) )\d{4})/;

  function form_Validation(thisform) {

    with (thisform) {
   
        var SSN = SSN.value;

            if (SSN.length > 0) {
      
            if (altSSN.test(SSN)) {

                  SSN = SSN.replace(altPattern, "$1-$2-$3");
                  SSN.value = SSN;
            }

            if (!reSSN.test(SSN)) {
                  alert("Social Security Number is not correct\nPlease use XXX-XX-XXXX\n");
                  SSN.focus();
                  return false;
            }
      }
      }
      return true;
}
</script>
dodgerfanAsked:
Who is Participating?
 
Brad HoweDevOps ManagerCommented:
Please take a read through the following article.

http://www.techrepublic.com/article/tie-javascript-events-to-aspnet-objects/5031730

Essentially, JS and ASP.net is not the same combination as old HTML/Classic ASP integration. You need to add the script to the form and add the attribute.

butSubmit.Attributes.Add("onClick", "return form_Validation();")

Cheers,
Hades666
0
 
sarabhaiCommented:
why u are not using validation control on the button for Social Security Number.
0
 
sarabhaiCommented:
just use CompareValidator Control
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.