Results 1 to 6 of 6

Thread: regex validation

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default regex validation

    Hi,

    I've got a Regex for url, which is:
    /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/

    How can I use it to check an input text in my form, (with an alert pop-up)?

    Thanks

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    I don't think I (fully) understand you, but have a try:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    document.getElementById('test').onblur=function(){
    if(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/.test(this.value))alert('Congratulations. That\'s a correct URL.');
    else alert('Sorry. That\'s an incorrect URL.');
    }}
    </script>
    <label for="test">Type a URL here: </label><input type="text" id="test">
    The function fires onblur (meanwhile).

    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    d-machine (08-09-2008)

  4. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you!

    Is there a simple way to change it to only show the "Sorry. That\'s an incorrect URL." alert? (because I don't need the first alert and I don't know how to edit it..)?
    Last edited by d-machine; 08-09-2008 at 10:21 AM.

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Replace with this script instead:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    document.getElementById('test').onblur=function(){
    if(!(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/.test(this.value)))alert('Sorry. That\'s an incorrect URL.');
    }}
    </script>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. The Following User Says Thank You to rangana For This Useful Post:

    d-machine (08-09-2008)

  7. #5
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you !!

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

    Default

    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!

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
  •