开发者

regex validator, required field

开发者 https://www.devze.com 2023-02-13 00:05 出处:网络
I\'m trying to use the regular expression validator for a numeric ID field. The field needs to be a required 开发者_Go百科field of any number. Currently, I\'m using:

I'm trying to use the regular expression validator for a numeric ID field. The field needs to be a required 开发者_Go百科field of any number. Currently, I'm using:

="\d{1,}"

Shouldn't this make it so the user has to at least enter 1 digit?? If I hit the submit button with the field empty, it passes validation and posts back.. But if I enter non-numeric characters, it errors fine. If I wanted zero or more occurrences, I'd use: ="(\d{1,})?"

Why isn't this working? Do I need to use this in combination with a Required Field Validator? That would suck ><


Make sure you set the property ValidateEmptyText to true or else the CustomValidator will not fire for empty text.

EDIT: You can attach a javascript function to the CustomValidator to accomplish this since I don't think a RegularExpressionValidator will fire against an empty control. I have created a basic example to illustrate the solution:

<script type="text/javascript">
    function CheckMyText(sender, args) {
        var compare = RegExp("\\d{1,}");
        args.IsValid = compare.test(args.Value);
        return;
    }
</script>

<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<asp:Button ID="btnTest" runat="server" Text="Test" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Error!"
    ControlToValidate="txtTest" ValidateEmptyText="true"
    ClientValidationFunction="CheckMyText"></asp:CustomValidator>

I have tested it and it seems to work. Leave a comment if you require further assistance.


You still need to use a RequiredFieldValidator.


I'm not sure where the user is entering the IDs, but if the input field is TextBox control why don't you use something like this:

if (tbID.Text.Length != 0)
{
    //Logic goes here
}


When user clicks submit, you need to make sure that not only empty strings are captured, below is a regex that looks for any whitespace(tab,space etc) + matches if character is not a digit(0-9)

 Dim FoundMatch As Boolean
    Try
        FoundMatch = Regex.IsMatch(SubjectString, "\Dm/rld$/\s", RegexOptions.IgnoreCase Or RegexOptions.Multiline)

'put your code here
    Catch ex As ArgumentException

'syntax error in regular expression
    End Try

I believe you'll need to use postback on your page, if you decide to use RequiredFieldValidator you can use above regex expression for that as well

Hth


In case someone is not using a CustomValidator then you can have a RequiredFieldValidator and RegularExpressionValidator for the same control. Found this solution here: http://forums.asp.net/t/1230931.aspx . Normally, this results in the error messages being displaced for the second validator but there is a way to fix that. You just have to set the Display property to dynamic for both the validators. Now the error messages for both the validators are displayed in the same location. Example code:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="ErrorMsg" ControlToValidate="controlID"
ValidationExpression="regexExpression"
Display="Dynamic"></asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ErrorMessage="ErrorMsg" ControlToValidate="controlID"
Display="Dynamic"></asp:RequiredFieldValidator>`
0

精彩评论

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

关注公众号