Email and Link format check with javascript

Discussion in 'JavaScript' started by Kuna, Oct 17, 2009.

  1. #1
    I have form where user have to enter link and mail. Can someone please can write little script or tell me where to find javascript which check right format of mail and link
     
    Kuna, Oct 17, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
  3. Kuna

    Kuna Well-Known Member

    Messages:
    426
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    thanks. what about links?
     
    Kuna, Oct 17, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    dunno.

    email:<br />
    <input type="text" id="email" /><br />
    
    homepage:<br />
    <input type="text" id="url" /><br />
    <br />
    <input type="submit" id="check" value="check them" />
    
    (function() {
        // create the string prototypes in a closed scope.
        var formVerifier = {
            regex: {
                email: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,    // "
                url: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i
            },
            init: function() {
                String.prototype.isEmail = function() {
                    // defer to parent by name because 'this' won't be available to prototype
                    return this.match(formVerifier.regex.email);
                };
    
                String.prototype.isURL = function() {
                    return this.match(formVerifier.regex.url);
                };
            }
        };
    
        formVerifier.init();
    })();
    
    window.onload = function() {
        var fields = {
            email: document.getElementById("email"),
            homepage: document.getElementById("url")
        };
    
        document.getElementById("check").onclick = function() {
            if (!fields.email.value.isEmail()) {
                alert(fields.email.value + " is not a valid email.");
            }
    
            if (!fields.homepage.value.isURL()) {
                alert(fields.homepage.value + " is not a valid url.");
            }
        };
    
    };
    
    PHP:
    the problem with url verification is ... its unreliable. you an do http://bar and that's valid. which technically CAN be valid if for example, using a host file aliasing or a local domain lookup etc. otherwise, you need to really look for a TLD that ends the url and this means knowing all acceptable TLDS which is a bummer to do, i suppose.
     
    dimitar christoff, Oct 17, 2009 IP