0

I have this email form validation script:

    <script type="text/javascript" language="javascript">  
function validateForm(thisform){  
 if(thisform.Name.value=="") {  
   alert("Ooouuupppsss... You did not enter your Name.");  
   thisform.Name.focus();  
   return false;  
 }  
 if(thisform.Email.value=="") {  
   alert("Ooouuupppsss... You did not enter a valid Email Address.");  
   thisform.Email.focus();  
   return false;  
 }  
 if(thisform.Subject.value=="") {  
   alert("Ooouuupppsss... You did not enter your Subject.");  
   thisform.Subject.focus();  
   return false;  
 }  
 if(thisform.Message.value=="") {  
   alert("Ooouuupppsss... You did not enter your Message.");  
   thisform.Message.focus();  
   return false;  
 }  
}</script>  

Can someone please tell me what do I have to add in this script in order to make the users enter a valid email address. Also I would like in the rest of the fields to make users to enter text (not links).

I've tried to add different pieces of code which I found on different websites but they did not work and this is because I am not sure if I am adding them right.

Thank you for reading my request.

All the best, Andi

2 Answers2

0

See this post for regex urls: regular expression for url

See this post for email validation: Validate email address in JavaScript?

See this for X browser event listening, if would use jQuery, ie8 uses attach see this: Javascript add events cross-browser function implementation: use attachEvent/addEventListener vs inline events

I would recommend looping through the form inputs, and checking if its email and if its not run the regex against the link.

(function(){ 
var validateForm = function(form){
    var errors = [], inputs = form.getElementsByTagName('input');
    for(var input = 0; input<inputs.length; input++){
        var currentInput = inputs[input];
        if(currentInput.value === ''){
            // handle attributes here for error message, push error message
            if(currentInput.attribute('name') === 'email'){
                // handle email
                // push error message
            }
        }
    }
    return errors; 
}

var contactForm = document.getElementById('contact');
contactForm.addEventListener('submit', function(e){ 
    var errorMessages = validateForm(contactForm);
    if(errorMessages.length === 0){

    } else {
        e.preventDefault() // stop the form from submitting
        // handle your messages 
    }
}
}());
Community
  • 1
  • 1
Michael Benin
  • 4,317
  • 2
  • 23
  • 15
0

For e-mail checking you can use following code in else part after checking if e-mail is empty

function validateForm(){
    var email = document.getElementById("email").value;
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (email.search(emailRegEx) == -1) {
        alert("e-mail is not valid");
        return false;
    }
}

and for url with same logic you can use following regular expression

var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;

following is a working example based on your work, you can improve this code it is only for showing you how it should be.

    function validateForm(thisform){  
        if(thisform.Name.value=="") {  
            alert("Ooouuupppsss... You did not enter your Name.");  
            thisform.Name.focus();  
            return false;  
        }  
        else{
            var name = thisform.Name.value;
            if (!checkURL(name)) {
                alert("name cannot be a url");
                return false;
            }
        }
        if(thisform.Email.value=="") {  
           alert("Ooouuupppsss... You did not enter a valid Email Address.");  
           thisform.Email.focus();  
           return false;  
        }  
         else{
            var email = thisform.Email.value;
            var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
            if (email.search(emailRegEx) == -1) {
                alert("e-mail is not valid");
                return false;
            }
        }
         if(thisform.Subject.value=="") {  
           alert("Ooouuupppsss... You did not enter your Subject.");  
           thisform.Subject.focus();  
           return false;  
        }
            else{
                if (!checkURL(thisform.Subject.value)) {
                    alert("subject cannot contain a url");
                    return false;
                }
            }
         if(thisform.Message.value=="") {  
           alert("Ooouuupppsss... You did not enter your Message.");  
           thisform.Message.focus();  
           return false;  
        }  
        else{
            if (!checkURL(thisform.Message.value)) {
                alert("message cannot contain a url");
                return false;
            }
        }
    }
    function checkURL(url){
        var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;
        if (url.search(urlRegEx) == -1) {
            return true;
        }
        return false;
    }
Murat Güvenç
  • 111
  • 1
  • 5
  • Thank you very much for your answer. In my opinion your answer seems to be a good option. Unfortunately I am pretty new in this field and I cannot find out how to include your code in mine, i've tried several times and I only get errors. I am pretty sure that there is something that I am not doing right. I've added an "else" part after the "if email is empty" part, checked with w3school.com how to do that and it still does not work :( If you have some spare time can you help me please? Again thank you very! – Mosila Andrei Alexandru Aug 23 '13 at 18:33
  • I've added a working example if this doesn't work you should also post your html – Murat Güvenç Aug 25 '13 at 07:33
  • OK, I've added the code you gave me and unfortunately it is not working. Is it possible to be because of the .php file that is redirecting the information from the contact form into my email? the .php file also contains a small JavaScript which creates a small window with a "thank you" message after the form was submitted. I am almost sure that the HTML code is correct because everything is working fine until I add other commands to the JavaScript I inserted in my first post. Thank you again :) and I am really sorry that I bother you again with my problem. – Mosila Andrei Alexandru Aug 25 '13 at 11:07
  • If html part is fine then try to debug your code with alerts. Maybe you can find the broken part. – Murat Güvenç Aug 26 '13 at 14:42