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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validate URL and Keywords

    I want to validate an imdb URL and an keywords field who contain minimum 3 words separated by comma.
    This script valdate any url, but i want to validate url like this:
    var urlRegex = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

    if(url == "") {
    inlineMsg('url','<strong>Error</strong><br />You must enter an URL.',2);
    return false;
    }
    if(!url.match(urlRegex)) {
    inlineMsg('url','<strong>Error</strong><br />You have entered an invalid url.',2);
    return false;
    }
    Thx! Sorry for my english.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,929
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You do not make it clear what you mean by "like this" but the following will validate a suffix title followed by / followed by (any) 2 letters (upper or lower case) and 7 digits followed by /

    var urlRegex = /^http:\/\/[A-Z0-9\.-]{3,}\.[A-Z]{3}\/title\/[A-Z]{2}\d{7}\/$/i;

    Note the ^ and $ characters which mean "and nothing but". The /i switch means "ignore case".

    If www.imdb.com is a constant then you can simplify:-

    var urlRegex = /^http:\/\/www\.imdb\.com\/title\/[A-Z]{2}\d{7}\/$/i;

    But why not simply request the user to enter a title reference and add all the prefixes to make it an URL automatically?

    You second question:-

    Code:
    var x = "ab,bc,cd";
    x = x.replace(/[^a-z,]/gi,"");  //strip anything but a-z and comma
    if (!/(,.+,)/g.test(x)) {
    alert ("You must enter at least three words separated by commas");
    }

    Always go to other people’s funerals, otherwise they won’t come to yours.
    Last edited by Philip M; 06-01-2009 at 01:16 PM.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you Philip M for replay, the url verification is work greate, but when checking keywords and i press duble click on submit button, is ignore the verification and the form are submited.
    If you have another idea please tell me.
    Last edited by marian2oo7; 06-01-2009 at 02:05 PM. Reason: my english

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,929
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by marian2oo7 View Post
    thank you Philip M for replay, the url verification is work greate, but when checking keywords and i press duble click on submit button, is ignore the verification and the form are submited.
    If you have another idea please tell me.
    Well, you must wrap it in a function which you call onsubmit = "return check3()"

    Code:
    function check3() {
    var x = document.formname.fieldname.value;  
    x = x.replace(/[^a-z,]/gi,"");  //strip anything but a-z and comma
    if (!/(.+,.+,.+)/g.test(x)) {
    alert ("You must enter at least three words separated by commas");
    }
    return false;
    }


  •  

    Posting Permissions

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