开发者

Regex Validator not working

开发者 https://www.devze.com 2023-01-14 01:23 出处:网络
I have a div tag that contains a textbox and a submit button.I am trying to only have the submit button work if the text entered is a number (int or decimal).However when running the debugger, the onc

I have a div tag that contains a textbox and a submit button. I am trying to only have the submit button work if the text entered is a number (int or decimal). However when running the debugger, the onclick function is called no matter what text is entered. Any idea how I messed this up?

<div class="content">
<table class="inputForm">
  <tr>
   <th>Percentage:</th>
  <td>
   <asp:TextBox ID="VATAmount" runat="server"></asp:TextBox>
  </td>
  <td>
   <asp:LinkButton ID="VATSubmit" runat="server" SkinID="Button" OnClick="VAT_Click" Text="Submit"></asp:LinkButton>
  </td>
 </tr>
</table>
<i>50% should be entered as 50</i>                                    
  <cb:RequiredRegularExpressionVali开发者_如何学Godator ID="VATVerify" runat="server" ControlToValidate="VATAmount" 
Display="Static" ErrorMessage="The Percent should be written as a decimal." Text="*" ValidationGroup="Add"
ValidationExpression="[*0-9]*\.[*0-9]" Required="true">
  </cb:RequiredRegularExpressionValidator>

Also I should note that the * does appear when an incorrect value is entered, but not when a correct one is entered


It seems that the regular expression might not be correct. The following might work (I say might because regex patterns are always sneaky in their ways of failing):

^[0-9]*\.?[0-9]+$


Try something like this:

^[0-9]+(?:\.[0-9]+)?$


The original poster's provided regex is definitely wrong, but I'm not sure that's the true cause. The one provided by @Mark Wilkins works great.

My guess would be that your validation isn't firing at all. You might try setting the CausesValidation="True" for your LinkButton and see if that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消