Combine multiple javascript into one

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

  1. #1
    Can someone please combine those 2 scripts into one, so I have only 1 onSubmit function in form.

    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function Validate(form) {
        var v = new RegExp();
        v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
        if (!v.test(form["link"].value)) {
            alert("You must supply a valid URL.");
            return false;
        }
    }
    //-->
    </SCRIPT>
    
    Code (markup):

          <script language="javascript">
          function checkMail(email) {
            var mail = email.value;
            var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]*{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
            if(!reg.test(mail) || mail == "")
            {
              alert("Your email address isn't valid!");
              return false;
            }
            else {
            }
          }
          </script>
    
    Code (markup):
     
    Kuna, Oct 18, 2009 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function Validate(form,email) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(form["link"].value)) {
    alert("You must supply a valid URL.");
    return false;
    }
    var mail = email.value;
    var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]*{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
    if(!reg.test(mail) || mail == "")
    {
    alert("Your email address isn't valid!");
    return false;
    }
    else {
    }
    }
    //-->
    </SCRIPT>
     
    kmap, Oct 18, 2009 IP
  3. Kuna

    Kuna Well-Known Member

    Messages:
    426
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    thanks. can you just tell me how to write that in form onsubmit="WHAT?"
     
    Kuna, Oct 18, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

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

    Kuna Well-Known Member

    Messages:
    426
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    It says: "Email is not valid" but it still submits form
     
    Kuna, Oct 18, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    that's ok. return false - i thought you knew that and just wanted the regex help :)

        
         var fields = {
            email: document.getElementById("email"),
            homepage: document.getElementById("url")
        };
    
        document.getElementById("check").onclick = function() {
            var errors = false, errorFields = [];
            if (!fields.email.value.isEmail()) {
                alert(fields.email.value + " is not a valid email.");
                errors = true;
                errorFields.push(fields.email);
            }
    
            if (!fields.homepage.value.isURL()) {
                alert(fields.homepage.value + " is not a valid url.");
                errors = true;
                errorFields.push(fields.homepage);
            }
            
            // you can iterate through the errorFields array after and do things like, make the background red etc to show on screen whats wrong.
            if (errors === true)
                return false;
        };
    PHP:
    also, (com|ca|net|org|fr|us|qc.ca|gouv.qc.ca) ??? so a .co.uk fails? in fact, 99% of TLDS fail. nice idea! french canadians script is it? :D
     
    dimitar christoff, Oct 18, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    oh yes. when it's a form submit, then the event needs to change.

    your markup is:
    <form action="payment.php" method="get" name="formcheck" onsubmit="return formCheck(this)">
    
    
    <div class="tekst">*Link:</div><br>
    <input name="link" type="text" class="TextField" value="http://www." size="35"  id="url">
    <br><br>
    <div class="tekst">*Email:</div><br>
    <input name="email" type="text" class="TextField" size="35"  id="email">
    <input type="image" src="images/send-button.jpg" name="submit" value="submit"  id="check">
    
    </form>
    PHP:
    change it to:

    <form action="payment.php" method="get" name="formcheck" id="formcheck">
    
    
    <div class="tekst">*Link:</div><br>
    <input name="link" type="text" class="TextField" value="http://www." size="35"  id="url">
    <br><br>
    <div class="tekst">*Email:</div><br>
    <input name="email" type="text" class="TextField" size="35"  id="email">
    <input type="image" src="images/send-button.jpg" name="submit" value="submit">
    
    </form>
    
    ...
    
    <script type="text/javascript">
    (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("formcheck").onsubmit = function() {
            var errors = false, errorFields = [];
            if (!fields.email.value.isEmail()) {
                alert(fields.email.value + " is not a valid email.");
                errors = true;
                errorFields.push(fields.email);
            }
    
            if (!fields.homepage.value.isURL()) {
                alert(fields.homepage.value + " is not a valid url.");
                errors = true;
                errorFields.push(fields.homepage);
            }
            
            // you can iterate through the errorFields array after and do things like, make the background red etc to show on screen whats wrong.
            if (errors === true)
                return false;
        }; // end onsubmit event.
    
    
    }; // end onload
    
    </script>
    PHP:
    notice - assign an id to form, then handle the onsubmit.

    handling the click was for demonstration purposes. a click on a button may return false but the submit event still fires.
     
    dimitar christoff, Oct 18, 2009 IP
  8. Kuna

    Kuna Well-Known Member

    Messages:
    426
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #8
    thank you very much
     
    Kuna, Oct 18, 2009 IP