Geeks With Blogs
Gaurav Taneja Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.
 /// <summary>
    /// Apply Java Script Validation on Form
    /// </summary>
    private void ApplyValidation()
    {
        StringBuilder strClientScript = new StringBuilder();
        string strErrorMsg = string.Empty;

        strClientScript.Append(@" var frmvalidator  = new Validator
                    ('aspnetForm','" + btnSave.ClientID + "','','','" + lblErrorMessage.ClientID + "');");

        //Apply Validation on Content Title
        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001")
            .Replace(AdminConstantKeys.TildeCharacter, "User Name");
        strClientScript.Append("frmvalidator.addValidation('"
            + txtUserName.ClientID + "','" + AdminConstantKeys.Required
            + "','" + strErrorMsg + "');");

        //Apply validation for User Name
        strErrorMsg = CommonFunction.GetErrorMessage("ERR1027")
            .Replace(AdminConstantKeys.TildeCharacter, "User Name");
        strClientScript.Append("frmvalidator.addValidation('" +
            txtUserName.ClientID + "','alphanumeric','" + strErrorMsg + "');");

        //Apply validation on Content Subject
        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001")
            .Replace(AdminConstantKeys.TildeCharacter, "First Name");
        strClientScript.Append("frmvalidator.addValidation('" +
            txtFirstName.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, "Last Name");
        strClientScript.Append("frmvalidator.addValidation('" +
            txtLastName.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR3901");
        strClientScript.Append("frmvalidator.addValidation('" +
            ddlUserType.ClientID + "','" + ddlUserType.Items[0].Text +
            "','" + strErrorMsg + "');");

        //Apply validation for Date difference between Exp and Publish date
        strErrorMsg = txtStopDate.ClientID + "|" +
            CommonFunction.GetErrorMessage("ERR4608");
        strClientScript.Append("frmvalidator.addValidation('" +
            txtStartDate.ClientID + "','dateDifference','"
            + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001")
            .Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.EmailAddress);
        strClientScript.Append("frmvalidator.addValidation('"
            + txtEmail.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1028").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.EmailAddress);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtEmail.ClientID + "','email','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.PhoneNumberMsg);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtPhoneNumber.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");


        strErrorMsg = CommonFunction.GetErrorMessage("ERR1028").Replace
            ("~~", AdminConstantKeys.PhoneNumber);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtPhoneNumber.ClientID + "','" + AdminConstantKeys.PhoneNumberValidation +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.CompanyNameMsg);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtCompanyName.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");

        strErrorMsg = "Please select at least one service";
        strClientScript.Append("frmvalidator.addValidation('" +
            lstAvailableServices.ClientID + "','" + AdminConstantKeys.ItemCount +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.AdministrativeContactEmail);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtAdmContactEmail.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1028").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.AdministrativeContactEmail);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtAdmContactEmail.ClientID + "','email','" +
            strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.TechnicalContactEmail);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtTechContactEmail.ClientID + "','" + AdminConstantKeys.Required
            + "','" + strErrorMsg + "');");

        strErrorMsg = CommonFunction.GetErrorMessage("ERR1028").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.TechnicalContactEmail);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtTechContactEmail.ClientID + "','" + AdminConstantKeys.EmailValidation + "','" +
            strErrorMsg + "');");


        strErrorMsg = CommonFunction.GetErrorMessage("ERR1001").
            Replace(AdminConstantKeys.TildeCharacter, AdminConstantKeys.StartDate);
        strClientScript.Append("frmvalidator.addValidation('" +
            txtStartDate.ClientID + "','" + AdminConstantKeys.Required +
            "','" + strErrorMsg + "');");


        strErrorMsg = CommonFunction.GetErrorMessage("ERR1006");
        strClientScript.Append("frmvalidator.addValidation('" +
            ddlStatus.ClientID + "','" + ddlStatus.Items[0].Text +
            "','" + strErrorMsg + "');");

        //Register the Client javascript
        if (!string.IsNullOrEmpty(strClientScript.ToString()))
        {
            ClientScript.RegisterStartupScript(this.GetType(),
                AdminConstantKeys.ValidationScript, strClientScript.ToString(), true);
        }
    } Posted on Thursday, March 6, 2008 4:52 AM Validation | Back to top


Comments on this post: Javascript validation in CS file

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net