4 Replies - 7757 Views - Last Post: 02 April 2014 - 08:16 PM

#1 Black_Lion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-March 13

Custom validator: invalid input made valid

Posted 22 May 2013 - 12:26 PM

Hi all. I have been trying to validate a textbox so that it doesnt accept input less than 4 characters. I used the custom validator to do so. My webform contains a button, textbox, label and custom validator.

If a user enters an invalid word (less than 4 chars), it displays the validation error message. But the thing is it carries on running the program until the end. On my label, it displays "Number length is valid" although it is not :mellow:/>/> . Thanks in advance.


        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {

            if (args.Value.Length >=4)
                args.IsValid = true;

            else
                args.IsValid = false;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Number length valid";
        }




Is This A Good Question/Topic? 0
  • +

Replies To: Custom validator: invalid input made valid

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,023
  • Joined: 12-December 12

Re: Custom validator: invalid input made valid

Posted 22 May 2013 - 02:27 PM

I know nothing about this, but:

Quote

When you use a CustomValidator control, you must check the Page.IsValid property in any server-side processing to be sure that all the validation checks have passed. The IsValid property returns the cumulative state of all the validator controls on the page. This property is used to make sure that all validation checks have passed any server-side processing.


http://msdn.microsof...(v=vs.100).aspx

So I assume you need:

protected void Button1_Click(object sender, EventArgs e)
{
    if (Page.isValid) {
        Label1.Text = "Number length valid";
    } else {
        Label1.Text = "You done wrong!";
    }
}

..but I may be wrong :)
Was This Post Helpful? 1
  • +
  • -

#3 netuser1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-August 13

Re: Custom validator: invalid input made valid

Posted 08 August 2013 - 12:53 PM

View PostBlack_Lion, on 22 May 2013 - 12:26 PM, said:

Hi all. I have been trying to validate a textbox so that it doesnt accept input less than 4 characters. I used the custom validator to do so. My webform contains a button, textbox, label and custom validator.

If a user enters an invalid word (less than 4 chars), it displays the validation error message. But the thing is it carries on running the program until the end. On my label, it displays "Number length is valid" although it is not :mellow:/>/>/> . Thanks in advance.


        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {

            if (args.Value.Length >=4)
                args.IsValid = true;

            else
                args.IsValid = false;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Number length valid";
        }



Was This Post Helpful? 0
  • +
  • -

#4 TheAKB  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-November 08

Re: Custom validator: invalid input made valid

Posted 10 October 2013 - 08:06 AM

Andrewsw is correct,

the custom validation will display if args.isvalid is false however if you have other functionality that runs you need to preform a check if page.isvalid.

If you were just moving to a new screen for instance this wouldn't be need as the error being displayed stops the switch.

This post has been edited by TheAKB: 10 October 2013 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#5 ChristianRVA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 06-January 14

Re: Custom validator: invalid input made valid

Posted 02 April 2014 - 08:16 PM

You could always use a regular expression too. This one simply checks that min length is 5.

 

<asp:RegularExpression ID="Myregex" runat="server" ControlToValidate="MyTextBox" ValidationExpression=".{5}.*" 
     ErrorMessage="Textbox requires minimum of 5 characters" /> 



This post has been edited by ChristianRVA: 02 April 2014 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1