renchan
renchan

Reputation: 519

Validation of a textbox, so that the value differed from another textbox

I want to validate a textbox, so that the Text value of firstNameTextBox differed from nicknameTextBox. I'm using InitialValue property of RequiredFieldValidator, like this -

 fieldValidatorInitialValue.InitialValue = firstNameTextBox.Text;

Here is the code:

        RequiredFieldValidator fieldValidatorInitialValue = new RequiredFieldValidator();
        TextBox firstNameTextBox = new TextBox(); 
        TextBox nicknameTextBox = new TextBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            Button submitButton = new Button();
            submitButton.CausesValidation = true;
            submitButton.Click += submitButton_Click;
            nicknameTextBox.ID = "nickname";
            firstNameTextBox.ID = "firstname";
            fieldValidatorInitialValue.ControlToValidate = firstNameTextBox.ID;
        }

        protected void submitButton_Click(object sender, EventArgs e)
        {
            fieldValidatorInitialValue.InitialValue = nicknameTextBox.Text;
        }

However, using this code the validation doesn't work correctly, only after the second click on button. I also tried to put all of the RequiredFieldValidator code to submitButton_Click event handler, however it doesn't work at all in this case, could someone please help me with it?

Upvotes: 0

Views: 67

Answers (3)

Vaibhav
Vaibhav

Reputation: 83

I think the way you are trying to compare "First name" and "Nick Name" is unnecessarily complex. Instead of that you can simply use CompareValidator and achieve the same task on single button click. Here , Add this on your designer page:

 <asp:CompareValidator ID="comNames" runat="server"
     ControlToValidate="firstNameTextBox"
     ControlToCompare="nicknameTextBox"
     ErrorMessage="Error: Name cant be same"
     SetFocusOnError="True" Text="*"></asp:CompareValidator>   

In case you want to add Validator dynamically, Add CompareValidator instead of RequiredFieldValidator and add following properties:

   ControlToValidate="firstNameTextBox"
ControlToCompare="nicknameTextBox"
ErrorMessage="Error: Name cant be same"
SetFocusOnError="True"
Text="*"   

Note: if you are using ValidationGroup for button,use same name of ValidationGroup for Validator also.

Upvotes: 0

Kashif
Kashif

Reputation: 3034

Try this.you should use CompareValidator instead of RequiredFieldValidator, with Operator="NotEqual"

 <asp:CompareValidator runat="server"
                  ControlToValidate="tbFName"
                  ControlToCompare="tbLName"
                  Type="String"
                  Operator="NotEqual"
                  ErrorMessage="First and last name cannot be the same" />

Upvotes: 2

Faisal
Faisal

Reputation: 29

Try below code

protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
         Button submitButton = new Button();
         submitButton.CausesValidation = true;
         submitButton.Click += submitButton_Click;
         nicknameTextBox.ID = "nickname";
         firstNameTextBox.ID = "firstname";
         fieldValidatorInitialValue.ControlToValidate = firstNameTextBox.ID;
       }
    }

Upvotes: 0

Related Questions