Link to home
Start Free TrialLog in
Avatar of OHHIITSSEAN
OHHIITSSEANFlag for United States of America

asked on

asp.net client side validation by calling page method

Dear asp.net gurus:

I have a asp.net web page where I need user enter product serial number, and I need to validate the user's entry; if the serial number already exists in SQL server database, then asp.net validator will display error message like 'serial number has been used'. I do not want to do this check on the clientvalidation. I knew that can be done by creating a javascript calling a asp.net static page method and then return the isvalid value to validator. The isvalid value is exchange by using JSON serialization?

I just wonder if anybody can give me some example and source code  to show me how to implement the way I want it to be.

Please do not just copy and paste some links to me, please give me some explananation and then I can know how to integrate with my project.

Thanks in advance.

Shawn


Avatar of saritapatel
saritapatel

textbox validation at serverside
------------------------------------------------------------


using System.Text.RegularExpressions;


#region securitycodebysarita
      int Invalidtext;
    Regex rx = new Regex("[^a-zA-Z0-9:$@.!_/\\~,#&*()=+{}<>?| \\s]");
   
   private void Txt_Validation(Control ControlContainer)
    {
        try
        {
            foreach (Control Ctl in ControlContainer.Controls)
            {
                if ((Ctl.Controls != null))
                {
                    Txt_Validation(Ctl);
                    if (Ctl is System.Web.UI.WebControls.TextBox)
                    {
                        try
                        {
                            TextBox tx = new TextBox();
                            tx = (TextBox)Ctl;

                            if ((rx.IsMatch(tx.Text)))
                            {
                                Invalidtext = Invalidtext + 1;
                             
                                string StrScript = "";
                                StrScript = "<script type='text/javascript'>alert('Invalid Input Character!'); </script>";
                                System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "", StrScript, false);

                                return;
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

   
    #endregion
 ******************put this code before any save or update.this will validate all your textbox ******************
   
     Invalidtext = 0;
        Txt_Validation(this);
        if (Invalidtext > 0)
        {
            Invalidtext = 0;
            return;
        }

Note : Change new Regex("[^a-zA-Z0-9:$@.!_/\\~,#&*()=+{}<>?| \\s]"); as per your requirement.
Avatar of OHHIITSSEAN

ASKER

Hello saritapate:

Thanks for your prompt response.

Like I said, I need to check if the serial number exists in the serial number registration table of SQL server, your workaround is not the implementation I want. I need a something like asp:customvalidator onclientvalidation calling a javascript whose function can call asp.net static page method, the page method can communication with SQL server, checking if the record already exists or not, then sending the ture/false value to javascript and then javascript can determine if isvalid is true or false and notify customvalidator.

Thanks for your time and support.

Sean
Hello ASP.Net gurus:

Anyone have ideas?

ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks ddayx10.