简体   繁体   中英

regex to match ONLY integers and not any strings not working

I have a utility of Cart in my website which is built in ASP.NET. In the cart page like it is generally on every page, I multiply quantity * price to calculate total of individual items in the cart.

For the quantity , I have a <asp:TextBox .... TextMode="Number"/> control. And have used a regex in a <asp:RegularExpressionValidator.../> .

The following is the ASPX code :

<asp:TextBox ID="txtQty" runat="server" Text="1" TextMode="Number" Style="width: 45px; text-align: center; margin-left: 10px"></asp:TextBox>

 <asp:RegularExpressionValidator ID="regExQty" runat="server" ControlToValidate="txtQty" ValidationExpression="^[1-9][0-9]*$" ErrorMessage="Invalid Quantity!" Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>

Now using this regex it functions properly for the following inputs :

1) doesn't allow '0' as input

2) doesnt' allow negative numbers

But it allows the following :

1) the input : '1pppp' , ie a digit following by any number/s of character

2) the input : 'p' , ie only string is also allowed.

I do not know why is this happening.

NOTE : I have used a repeater control, inside which there is a text box used for quantity.

UPDATE: This is the code to calculate the total amount :

int total = 0;

        for (int i = 0; i < rptr.Items.Count; i++)
        {
            TextBox txtBox = (TextBox)rptr.Items[i].FindControl("txtQty");
            HiddenField hdnProductId = (HiddenField)rptr.Items[i].FindControl("hdnVendorItemID");

            VendorItemLogic vendorItemLogic = new VendorItemLogic();
            VendorItem vendorItem = vendorItemLogic.SelectByID(Convert.ToInt32(hdnProductId.Value));
            int price = Convert.ToInt32(vendorItem.Price);

            total += Convert.ToInt32(txtBox.Text) * price;

            lblTotal.Text = String.Format("{0:#,###,###.##}",(object)total);
            plcHldrTotal.Visible = true;

        }

这是在我从文本框中删除光标之前发生的情况

This is what happens , it doesn't give an error message but changes the border color to red, and then again when I move my cursor out of it, it removes the border color.

It is true that in ASP.NET 4.5, the TextMode attribute can have more than 3 values ("SingleLine", "MultiLine", or "Password"). However, these are not yet supported by all Web browsers.

A quote from codeproject :

All these new options will only work with browsers supporting these HTML5 features (for example: Internet Explorer 10, Chrome, and Opera). For all other browsers (earlier versions of Internet Explorer and Firefox), these textboxes will behave as normal textboxes.

So, to make your code comaptible with all browsers, you should revert the "Number" value by either removing the attribute completely, or setting to one of the allowed values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM