Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Brighton, UK
    Posts
    282
    Thanks
    1
    Thanked 0 Times in 0 Posts

    .NET validation allow numbers and blank/empty

    Hi i am tryin g to set a validation on a box but only allow number or leave blank has anyone got any ideas how i can do this ?? here is the code i am using..


    <ValidationSettings ErrorDisplayMode="ImageWithTooltip" SetFocusOnError="True" ValidationGroup="AddNewCard">
    <RegularExpression ErrorText="Numbers only required, no spaces... example 0308."
    ValidationExpression="^([0-9]{4,4})$*" />
    <RequiredField ErrorText="Start date required..." IsRequired="True" />
    </ValidationSettings>

    cheers

    chris
    <marquee>thanks</marquee>

  • #2
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Are you using sometype of 3th party control?

    This looks like RadGrid for ASP.NET or someother 3th party control.
    I was wondering why frisbees got bigger as they got closer, then it hit me.

  • #3
    Regular Coder
    Join Date
    May 2007
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Are you really using that code or is it a poorly copied example and you are really using standard ASP.NET server controls (hence Freon22's confusion)?

    If so, then your RegularExpressionValidator should look something like this:

    <asp:RegularExpressionValidator ID="whatever" runat="server" ControlToValidate="whatever"
    ValidationExpression="^[0-9]{4}$"
    ErrorMessage="Numbers only required, no spaces... e.g., 0308" />


    This will not do any validation if the user enters nothing in the textbox, so the regular expression does not have to validate no input.

    If you really are using some weird and wonderful controls I know nothing about, at least my change to the validation expression may help you.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Brighton, UK
    Posts
    282
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi, i am alerady using the code sampled.. but this bit of code does not allow me to leave the field blank?? i have it so it can handle numbers only but i don't know how let it also allow a blank input box?

    so i need they can add 4 numbers or leave the input box blank / empty

    hope this gives you more detail...
    <marquee>thanks</marquee>

  • #5
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,100
    Thanks
    2
    Thanked 23 Times in 23 Posts
    You need to add this to make the field required:

    Code:
    <asp:RequiredFieldValidator ID="whatever" runat="server"
       ControlToValidate="whatever" ErrorMessage="Field is required"></asp:RequiredFieldValidator>
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #6
    Regular Coder
    Join Date
    May 2007
    Location
    UK
    Posts
    180
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I think the requirement is to accept a 4 digit number or a blank entry. The RequiredFieldValidator control will not allow a blank entry, so it should not be added to the page.

    Also, the original code example is using proprietary controls which I have never encountered before. The following validation expression might work:
    "^([0-9]{4})?$",
    but as I have never seen these controls (or their documentation) before, then I cannot be sure.

  • #7
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,100
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Quote Originally Posted by SouthwaterDave View Post
    I think the requirement is to accept a 4 digit number or a blank entry.
    My apologies. I misread what he was asking.

    Here's a way that will work which I personally tested, requiring the validation to be done in the code-behind:

    Note: You'll need to add this at the top of your code-behind:

    Code:
    using System.Text.RegularExpressions;
    Code:
    string regexString = @"^[0-9]{4}$";
    Regex regex = new Regex(regexString);
    
    if (txtSomething.Text.Length == 0)
    {
       lblSomething.Text = "Length is valid!";
    }
    else
    {
       if (regex.IsMatch(txtSomething.Text))
       {
          lblSomething.Text = "4-digit number!";
       }
       else
       {
          lblSomething.Text = "Not a 4-digit number!";
       }
    I've used txtSomething as the textbox to validate and lblSomething as a repository for the error message. Not complete code, of course, but just an example of the validation.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •