Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Validation Question with Control

Posted on 2007-08-07
11
Medium Priority
?
932 Views
Last Modified: 2008-02-01
Hello all.  I am having an issue where my regular expression validator is firing off the first time if it is wrong but after a submit happens on a calciulate it doesnt seem to fire ever again after that.  I need to explain and show my code so it makes some sense :).   I have a textbox on my form called txtRetailPrice.   This is the only textbox entry the user can do.   This has to be currency format and not allow characters etc.   Then I also have just 1 custom validator on my page that is validating that the price entered is greater than a certain calculated price once they click a calculate button beside the textbox.   When I go into the form if I enter characters into the box then it fires the regular expression and says it "must be currency".   Once I select the calculate button and it is a valid currency and outside the custom validator boundaries and ok , after that the only thing I have working is the custom validator at that point.  I think it has to do with the way I am using IsValid in my code and the Page.Validate in one place.  Here is what I have.

ASPX:
<asp:TextBox ID="txtRetailPrice" width="140" runat="server"></asp:TextBox>
<asp:Button CssClass="TableButton" ID="btnCalculate" runat="server" Text="Calculate" CausesValidation="true" OnClick="CalculatePrice" />
  <asp:RegularExpressionValidator ID="regexpRetailPrice" runat="server"    
                                    ErrorMessage="Must be currency ex. 0.00."
                                    ControlToValidate="txtRetailPrice"    
                                    ValidationExpression="^\d+(\.\d\d)?$" />
         <asp:CustomValidator id="CustomValidator1" runat="server" display="Dynamic" ErrorMessage="Retail Price must be in currency format and >= Retail Print + Distribution Cost" OnServerValidate="Validate_Retail"/>  

CODE BEHIND - CalculatePrice function and OnServerValidate Validate_Retail:
private void CalculateTotals()
    {
        try
        {
                if (isNumeric(txtRetailPrice.Text, System.Globalization.NumberStyles.Float))
                {
                    txtRetailPrice.Text = Convert.ToDouble(txtRetailPrice.Text).ToString("N");
                }
                else
                {
                    return;
                }

                CustomValidator1.IsValid = true;
                if (isNumeric(txtRetailPrice.Text, System.Globalization.NumberStyles.Float) && isNumeric(lblWebMinimumSalesPrice.Text, System.Globalization.NumberStyles.Float));
                {
                    double dblWebRoyalty = 0.00;
                    dblWebRoyalty = double.Parse(txtRetailPrice.Text) - double.Parse(lblWebMinimumSalesPrice.Text);
                    lblWebRoyalty.Text = String.Format("{0:F2}", dblWebRoyalty);
                    lblWebTotalPrice.Text = txtRetailPrice.Text;
                }

                if (isNumeric(txtRetailPrice.Text, System.Globalization.NumberStyles.Float) && isNumeric(lblRetailMinimumSalesPrice.Text, System.Globalization.NumberStyles.Float));
                {
                    double dblRetailRoyalty = 0.00;
                    dblRetailRoyalty = double.Parse(txtRetailPrice.Text) - double.Parse(lblRetailMinimumSalesPrice.Text);
                    lblRetailRoyalty.Text = String.Format("{0:F2}", dblRetailRoyalty);
                    lblRetailTotalPrice.Text = txtRetailPrice.Text;
                }

                Page.Validate();

        }
        catch (Exception ex)
        {
            RecordError(ex.Message);
            Response.Redirect("~/BookWork/Protected/Error.aspx");
        }
    }

    protected void Validate_Retail(object sender, ServerValidateEventArgs e)
    {
        if(isNumeric(txtRetailPrice.Text, System.Globalization.NumberStyles.Float) == false)
        {
             e.IsValid = false;
             return;
        }

        if (double.Parse(txtRetailPrice.Text) < double.Parse(lblRetailMinimumSalesPrice.Text))
        {
                e.IsValid = false;
                return;
        }
        else
        {
            e.IsValid = true;
            return;
        }
    }

.....
I am sure I am doing something wrong with the custom validator stuff that is causing this most likely.  The top sections here I check txtRetailPrice IsNumeric now is because its always getting into the code after the calculate button goes through and is fine then it always enters the code behind and I am into the functions.  The regular expression is only working the first time if I enter characters in the textbox.  I now have it always just firing off the custom validator text if they enter bad characters after the first time it works.  This works fine but I know its not totally correct.   Anytime I enter bad characters into the textbox it should fire off my regular expression first I beleive.
0
Comment
Question by:sbornstein2
  • 5
  • 5
11 Comments
 
LVL 14

Expert Comment

by:chinu1310
ID: 19646324
Try putting validation in  " ispostback "  block.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 19648847
Why not just use a range validator:

<asp:Rangevalidator id="somevalidator" runat="server"  display="Dynamic" ErrorMessage="" controltovalidate="txtRetailPrice" type="Currency"/>


Page_Load()
{
      somevalidator.ErrorMessage = "Must be between " SomeAmount + " and " + SomeOtherAmount;
      somevalidator.Maximum = SomeAmount;
      somevalidator.Minimum = SomeOtherAmount;
}
0
 

Author Comment

by:sbornstein2
ID: 19683934
strick well the minimum has to be greater than of equal to a value I have in a label control text, the maximum can be whatever the user wants.   How do I handle this?  For example:

lblMyMinPrice.Text = "7.88";
txtRetailPrice.Text = has to be => than the MinPrice.  

How can I do this with a range validator or something other than a custom?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:chinu1310
ID: 19684305
You need to create the dynamic range validator control object and than you can assign minimum value to it.
http://msdn2.microsoft.com/en-us/library/aa478956.aspx

Hope it helps
0
 

Author Comment

by:sbornstein2
ID: 19684603
yes but what do I do, leave the maximum blank?  I have everything you said above and in my code.  So do i just set the minimum and no max?  If so will that let there be any max?
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 19684671
Yeah. Max is when you have uper bound. If you don't have any uper bound leave it blank.
0
 

Author Comment

by:sbornstein2
ID: 19684907
doesnt like that

The value '' of the MaximumValue property of 'rngRetailValidator' cannot be converted to type 'Currency'.
0
 

Author Comment

by:sbornstein2
ID: 19684920
this is what I have so far:

ASPX:
<asp:Rangevalidator id="rngRetailValidator" runat="server"  display="Dynamic" ErrorMessage="Retail Price must be >= Retail Print + Distribution Cost" controltovalidate="txtRetailPrice" type="Currency"/>

Code Behind:
 rngRetailValidator.MinimumValue = lblRetailMinimumSalesPrice.Text;
0
 
LVL 14

Accepted Solution

by:
chinu1310 earned 2000 total points
ID: 19685008
Can you make any upper bound. I mean a certain level that will never reach as a maximum value in your application. Than we can put that as a maximum value.
0
 

Author Comment

by:sbornstein2
ID: 19685112
i guess I can
0
 
LVL 14

Expert Comment

by:chinu1310
ID: 19685487
Good. Glad to help.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question