Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Exact character match validation

  1. #1
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exact character match validation

    Hi expert, how to write the code of exact character match in a form.
    For example I have a text field with 1 character length, and the user can only type in a or b, otherwise the form will pop up errror message. I tried the exact character match like ([Aa]) |([Bb]), but it doesn't work. How to get it correct. Thanks in advance!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't understand. If you have:
    Code:
    <form action="">
      <input type="text" name="one" maxlength="1">
    </form>
    You can just use:
    Code:
    if(document.forms[0].elements['one'].value == "a" || document.forms[0].elements['one'].value == "b") {
      // do whatever
    } else {
      window.alert("TEH ERROR");
    }
    However, <select> elements are better suited to this task.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey! But I wrote this program in asp.net. I'm using ValiValidationExpression="[a]||[b]", but it doesn't work.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In that case, why are you posting in the Javascript forum?

    Insofar as I know, the only person here who knows ASP.NET is Otaku, who only pops on from time to time.

    If that's regex, however, try (a|b).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ValidationExpression="[abAB]" will do the job.

  6. #6
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks! But it doesn't work. The system didn't display any error and let me go when I enter other letters or numbers other than a or b. Would you please tell me what's wrong? Thanks!

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    No idea myself, but to save you some time when someone who does checks out this thread,
    you should post a link to your page and post your source code.
    that will make it a lot easier than guessing at what may or may not be wrong.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Like djr33 wrote, it's hard to say what's wrong without any code. Here is a small working sample:

    Code:
    <%@ Page Language="VB" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        Protected Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Response.Redirect("http://www.dynamicdrive.com")
        End Sub
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Validation Expression Sample</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="MyTextBox" runat="server"></asp:TextBox>
                <asp:Button ID="MyButton" runat="server" Text="GO!" OnClick="MyButton_Click" />
                <asp:RegularExpressionValidator 
                    ID="RegExValidator" 
                    runat="server" 
                    ControlToValidate="MyTextBox"
                    ErrorMessage="Your error message here!" 
                    ValidationExpression="[abAB]"></asp:RegularExpressionValidator>
            </div>
        </form>
    </body>
    </html>
    Make sure your ControlToValidate match your TextBox ID

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Presuming that conforms to XML rules, you can self-close the asp:RegularExpressionValidator and asp:TextBox tags too.
    What does an asp:Button render down into?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Apr 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    it does not matter if you self close or not in the ASP.Net code, both version will produce the same HTML output:

    <input ...... />

Bookmarks

Posting Permissions

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