isvalid not working for a text box

Discussion in 'ASP .Net' started by =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 23, 2004.

  1. Hi, I have a text box with corresponding comparevalidator as so..

    <tr><td align="right">End Date :</td>
    <td><asp:TextBox id="dtEndDate" runat="server" onTextChanged="set_Duration"
    autopostback="true" cssClass="mandatory"/></td>
    <td><asp:CompareValidator id="vdEndDate" runat="server"
    cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    </td></tr>

    As you can see when the user changes the text in dtEndDate, i want some code
    to fire. However I only want this code to fire if they have entered a correct
    date format, tested by vdEndDate, my validator control. So in my script to
    fire I have this:

    public void set_Duration(Object sender, EventArgs e) {
    if (vdEndDate.IsValid) {
    blah blah
    }
    }

    But when i put in a silly date and exit the field, the code still fires and
    errors coz my date format is wrong for the code i've written. Why is it not
    escaping the if condition?? I even see the validator control error message
    pop up briefly before it bombs. So surely vdEndDate is not valid when it
    enters my code????
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 23, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Dan Bass Guest

    Should that not be

    if ( e.IsValid )
    {
    //....
    }

    instead of vdEndDate?

    "louise raisbeck" <> wrote in
    message news:...
    > Hi, I have a text box with corresponding comparevalidator as so..
    >
    > <tr><td align="right">End Date :</td>
    > <td><asp:TextBox id="dtEndDate" runat="server"
    > onTextChanged="set_Duration"
    > autopostback="true" cssClass="mandatory"/></td>
    > <td><asp:CompareValidator id="vdEndDate" runat="server"
    > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    > </td></tr>
    >
    > As you can see when the user changes the text in dtEndDate, i want some
    > code
    > to fire. However I only want this code to fire if they have entered a
    > correct
    > date format, tested by vdEndDate, my validator control. So in my script to
    > fire I have this:
    >
    > public void set_Duration(Object sender, EventArgs e) {
    > if (vdEndDate.IsValid) {
    > blah blah
    > }
    > }
    >
    > But when i put in a silly date and exit the field, the code still fires
    > and
    > errors coz my date format is wrong for the code i've written. Why is it
    > not
    > escaping the if condition?? I even see the validator control error message
    > pop up briefly before it bombs. So surely vdEndDate is not valid when it
    > enters my code????
    Dan Bass, Nov 23, 2004
    #2
    1. Advertising

  3. I think you meant sender not the eventargs parameter ??? might be wrong.

    But no it doesnt like that anyway.

    It says in my ASP.NET book you can check to see if individual controls are
    valid by

    [validatorcontrolid].IsValid

    Why does it bypass the if statement ?!

    "Dan Bass" wrote:

    > Should that not be
    >
    > if ( e.IsValid )
    > {
    > //....
    > }
    >
    > instead of vdEndDate?
    >
    > "louise raisbeck" <> wrote in
    > message news:...
    > > Hi, I have a text box with corresponding comparevalidator as so..
    > >
    > > <tr><td align="right">End Date :</td>
    > > <td><asp:TextBox id="dtEndDate" runat="server"
    > > onTextChanged="set_Duration"
    > > autopostback="true" cssClass="mandatory"/></td>
    > > <td><asp:CompareValidator id="vdEndDate" runat="server"
    > > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    > > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    > > </td></tr>
    > >
    > > As you can see when the user changes the text in dtEndDate, i want some
    > > code
    > > to fire. However I only want this code to fire if they have entered a
    > > correct
    > > date format, tested by vdEndDate, my validator control. So in my script to
    > > fire I have this:
    > >
    > > public void set_Duration(Object sender, EventArgs e) {
    > > if (vdEndDate.IsValid) {
    > > blah blah
    > > }
    > > }
    > >
    > > But when i put in a silly date and exit the field, the code still fires
    > > and
    > > errors coz my date format is wrong for the code i've written. Why is it
    > > not
    > > escaping the if condition?? I even see the validator control error message
    > > pop up briefly before it bombs. So surely vdEndDate is not valid when it
    > > enters my code????

    >
    >
    >
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 23, 2004
    #3
  4. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Dan Bass Guest

    The easiest way to debug javascript is to litter it with alerts...

    put an alert just into your function to check it's firing, then an alert
    (vdEndDate) to check it's a valid object, then alert (vdEndDate.IsValid),
    etc... this'll help you narrow down which parameter is causing the problems.

    Javascript just stops executing if there's an error, which can be annoying,
    so another option is try/catch while also using alerts to check where your
    functions running too...

    Good luck.

    Dan.

    "louise raisbeck" <> wrote in
    message news:...
    >I think you meant sender not the eventargs parameter ??? might be wrong.
    >
    > But no it doesnt like that anyway.
    >
    > It says in my ASP.NET book you can check to see if individual controls are
    > valid by
    >
    > [validatorcontrolid].IsValid
    >
    > Why does it bypass the if statement ?!
    >
    > "Dan Bass" wrote:
    >
    >> Should that not be
    >>
    >> if ( e.IsValid )
    >> {
    >> //....
    >> }
    >>
    >> instead of vdEndDate?
    >>
    >> "louise raisbeck" <> wrote in
    >> message news:...
    >> > Hi, I have a text box with corresponding comparevalidator as so..
    >> >
    >> > <tr><td align="right">End Date :</td>
    >> > <td><asp:TextBox id="dtEndDate" runat="server"
    >> > onTextChanged="set_Duration"
    >> > autopostback="true" cssClass="mandatory"/></td>
    >> > <td><asp:CompareValidator id="vdEndDate" runat="server"
    >> > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    >> > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    >> > </td></tr>
    >> >
    >> > As you can see when the user changes the text in dtEndDate, i want some
    >> > code
    >> > to fire. However I only want this code to fire if they have entered a
    >> > correct
    >> > date format, tested by vdEndDate, my validator control. So in my script
    >> > to
    >> > fire I have this:
    >> >
    >> > public void set_Duration(Object sender, EventArgs e) {
    >> > if (vdEndDate.IsValid) {
    >> > blah blah
    >> > }
    >> > }
    >> >
    >> > But when i put in a silly date and exit the field, the code still fires
    >> > and
    >> > errors coz my date format is wrong for the code i've written. Why is it
    >> > not
    >> > escaping the if condition?? I even see the validator control error
    >> > message
    >> > pop up briefly before it bombs. So surely vdEndDate is not valid when
    >> > it
    >> > enters my code????

    >>
    >>
    >>
    Dan Bass, Nov 23, 2004
    #4
  5. i'm not writing javascript its C# on the server side checking if the web
    control is valid!



    "Dan Bass" wrote:

    > The easiest way to debug javascript is to litter it with alerts...
    >
    > put an alert just into your function to check it's firing, then an alert
    > (vdEndDate) to check it's a valid object, then alert (vdEndDate.IsValid),
    > etc... this'll help you narrow down which parameter is causing the problems.
    >
    > Javascript just stops executing if there's an error, which can be annoying,
    > so another option is try/catch while also using alerts to check where your
    > functions running too...
    >
    > Good luck.
    >
    > Dan.
    >
    > "louise raisbeck" <> wrote in
    > message news:...
    > >I think you meant sender not the eventargs parameter ??? might be wrong.
    > >
    > > But no it doesnt like that anyway.
    > >
    > > It says in my ASP.NET book you can check to see if individual controls are
    > > valid by
    > >
    > > [validatorcontrolid].IsValid
    > >
    > > Why does it bypass the if statement ?!
    > >
    > > "Dan Bass" wrote:
    > >
    > >> Should that not be
    > >>
    > >> if ( e.IsValid )
    > >> {
    > >> //....
    > >> }
    > >>
    > >> instead of vdEndDate?
    > >>
    > >> "louise raisbeck" <> wrote in
    > >> message news:...
    > >> > Hi, I have a text box with corresponding comparevalidator as so..
    > >> >
    > >> > <tr><td align="right">End Date :</td>
    > >> > <td><asp:TextBox id="dtEndDate" runat="server"
    > >> > onTextChanged="set_Duration"
    > >> > autopostback="true" cssClass="mandatory"/></td>
    > >> > <td><asp:CompareValidator id="vdEndDate" runat="server"
    > >> > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    > >> > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    > >> > </td></tr>
    > >> >
    > >> > As you can see when the user changes the text in dtEndDate, i want some
    > >> > code
    > >> > to fire. However I only want this code to fire if they have entered a
    > >> > correct
    > >> > date format, tested by vdEndDate, my validator control. So in my script
    > >> > to
    > >> > fire I have this:
    > >> >
    > >> > public void set_Duration(Object sender, EventArgs e) {
    > >> > if (vdEndDate.IsValid) {
    > >> > blah blah
    > >> > }
    > >> > }
    > >> >
    > >> > But when i put in a silly date and exit the field, the code still fires
    > >> > and
    > >> > errors coz my date format is wrong for the code i've written. Why is it
    > >> > not
    > >> > escaping the if condition?? I even see the validator control error
    > >> > message
    > >> > pop up briefly before it bombs. So surely vdEndDate is not valid when
    > >> > it
    > >> > enters my code????
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 23, 2004
    #5
  6. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Peter Blum Guest

    On the server side, there is an order of execution.
    Page_Load
    TextChanged
    OnClick
    PreRender

    Validation automatically fires during the OnClick event, due to the Button
    control calling Page.Validate() for you. Prior to this point, all validators
    are at their default state of IsValid=true.

    In your TextChange method, simply do this:
    vdEndDate.Validate();
    if (vdEndDate.IsValid) ....

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "louise raisbeck" <> wrote in
    message news:...
    > Hi, I have a text box with corresponding comparevalidator as so..
    >
    > <tr><td align="right">End Date :</td>
    > <td><asp:TextBox id="dtEndDate" runat="server"
    > onTextChanged="set_Duration"
    > autopostback="true" cssClass="mandatory"/></td>
    > <td><asp:CompareValidator id="vdEndDate" runat="server"
    > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    > </td></tr>
    >
    > As you can see when the user changes the text in dtEndDate, i want some
    > code
    > to fire. However I only want this code to fire if they have entered a
    > correct
    > date format, tested by vdEndDate, my validator control. So in my script to
    > fire I have this:
    >
    > public void set_Duration(Object sender, EventArgs e) {
    > if (vdEndDate.IsValid) {
    > blah blah
    > }
    > }
    >
    > But when i put in a silly date and exit the field, the code still fires
    > and
    > errors coz my date format is wrong for the code i've written. Why is it
    > not
    > escaping the if condition?? I even see the validator control error message
    > pop up briefly before it bombs. So surely vdEndDate is not valid when it
    > enters my code????
    Peter Blum, Nov 23, 2004
    #6
  7. i see. Very helpful to give me the order of exec, thanks so much.

    On the same vein, I have been also getting a javascript error
    Page..Validators.isvalid is null or not an object (something like that I'm at
    home now) I wondered if this solution will solve that? I have had to disable
    client scripts just to avoid it, obviously not ideal. Ring any bells ??

    "Peter Blum" wrote:

    > On the server side, there is an order of execution.
    > Page_Load
    > TextChanged
    > OnClick
    > PreRender
    >
    > Validation automatically fires during the OnClick event, due to the Button
    > control calling Page.Validate() for you. Prior to this point, all validators
    > are at their default state of IsValid=true.
    >
    > In your TextChange method, simply do this:
    > vdEndDate.Validate();
    > if (vdEndDate.IsValid) ....
    >
    > --- Peter Blum
    > www.PeterBlum.com
    > Email:
    > Creator of "Professional Validation And More" at
    > http://www.peterblum.com/vam/home.aspx
    >
    > "louise raisbeck" <> wrote in
    > message news:...
    > > Hi, I have a text box with corresponding comparevalidator as so..
    > >
    > > <tr><td align="right">End Date :</td>
    > > <td><asp:TextBox id="dtEndDate" runat="server"
    > > onTextChanged="set_Duration"
    > > autopostback="true" cssClass="mandatory"/></td>
    > > <td><asp:CompareValidator id="vdEndDate" runat="server"
    > > cssClass="ErrorMessage" ControlToValidate="dtEndDate" Type="Date"
    > > Operator="DataTypeCheck" ErrorMessage="Incorrect date format"/>
    > > </td></tr>
    > >
    > > As you can see when the user changes the text in dtEndDate, i want some
    > > code
    > > to fire. However I only want this code to fire if they have entered a
    > > correct
    > > date format, tested by vdEndDate, my validator control. So in my script to
    > > fire I have this:
    > >
    > > public void set_Duration(Object sender, EventArgs e) {
    > > if (vdEndDate.IsValid) {
    > > blah blah
    > > }
    > > }
    > >
    > > But when i put in a silly date and exit the field, the code still fires
    > > and
    > > errors coz my date format is wrong for the code i've written. Why is it
    > > not
    > > escaping the if condition?? I even see the validator control error message
    > > pop up briefly before it bombs. So surely vdEndDate is not valid when it
    > > enters my code????

    >
    >
    >
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Nov 23, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike P

    Page.IsValid

    Mike P, Jun 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,385
    Mike P
    Jun 25, 2003
  2. DesignerX
    Replies:
    1
    Views:
    351
    Joe Molloy
    Dec 18, 2003
  3. Drunken Coder

    Page.IsValid behavior

    Drunken Coder, Jan 7, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    815
    Peter Blum
    Jan 9, 2004
  4. Gnarlodious
    Replies:
    4
    Views:
    17,589
    elizas
    May 5, 2010
  5. Page.IsValid Not Working

    , Jul 13, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    543
    xazos79
    Jul 16, 2007
Loading...

Share This Page