3

I have following code which validates the value of a textbox to make sure it's not blank but I also need to check that it does not equal the initial value of the textbox (defaultValue).

Here's what I have so far...

Javascript:

function textValidation(source, arguments)
   {
        if ( arguments.Value != "" ){ // && arguments.Value != arguments.defaultValue
            arguments.IsValid = true;
        } else {
            $(source).parents("div").css({"background-color":"red"});
            arguments.IsValid = false;
        }
   }

.net

  <asp:TextBox runat="server" ID="Initial" Text="Initial" defaultValue="Initial" Width="120px" />                               

<asp:CustomValidator id="Initial_req"
     ControlToValidate="Initial"
     ClientValidationFunction="textValidation"
     ValidateEmptyText="true"
     runat="server"
     CssClass="errorAsterisk"
     Text="*" 
     ErrorMessage="Complete all correspondence fields" />

3 Answers 3

10

You can do what you want using a CSS class to identify the TextBox and retrieve it with jQuery, allowing you to obtain the attribute defaultValue:

Markup:

<asp:TextBox runat="server" 
             ID="Initial" 
             Text="Initial" 
             defaultValue="Initial" 
             Width="120px" 
             ValidationGroup="Test" 
             CssClass="to-validate" />

<asp:CustomValidator ID="Initial_req" 
   ControlToValidate="Initial" 
   ClientValidationFunction="textValidation"
   ValidateEmptyText="true" 
   runat="server" 
   CssClass="errorAsterisk" 
   Text="*" 
   ErrorMessage="Complete all correspondence fields"
   ValidationGroup="Test" />

<asp:Button ID="btnValidate" runat="server" Text="Validate" ValidationGroup="Test" />

Javascript:

function textValidation(source, arguments) {
     var initialValue = $(source).siblings(".to-validate:first").attr("defaultValue");

     if (arguments.Value != "" && arguments.Value != initialValue) { // && arguments.Value != arguments.defaultValue
         arguments.IsValid = true;
     } else {
         $(source).parents("div").css({ "background-color": "red" });
         arguments.IsValid = false;
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

1)You not pass source and arguments parameters in your function .

2) then call this syntax way for ClientValidationFunction="javascript:textValidation(param1,oaram2);return true;"

3)you can use required field validation in text box here

4)or another way is (we dont use any parameter ) for here

Comments

0

A RequiredFieldValidator will prevent blanks as well as initial values.

Use attribute InitialValue.

See here:

Use it to prevent any value that you like being entered into a TextBox. The TextBox doesn't have to have that value embedded at the start - it just can't be submitted with that value.

3 Comments

This will require two RequiredFieldValidators - one to validate "Initial" and other to validate "".
This was my initial approach, but as you'll see from my code, I want to be able to change the background color of a parent element so needed to use the CustomValidator. Thanks for the advice though.
@MarcusVinicius - no, one validator will do both blank and initial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.