6

This is the script I use for form validation:

<script language="JavaScript">

function formCheck(formobj){
    var fieldRequired = Array("Name", "Email", "Phone", "comments");
    var fieldDescription = Array("Name", "Email", "Phone", "Comments");
    var alertMsg = "Please complete the following fields:\n";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "text":
            case "textarea":
                if (obj.value == "" || obj.value == null){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            default:
            }
            if (obj.type == undefined){
                var blnchecked = false;
                for (var j = 0; j < obj.length; j++){
                    if (obj[j].checked){
                        blnchecked = true;
                    }
                }
                if (!blnchecked){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
}
// -->
</script>


<form onsubmit="return formCheck(this);" action="/capture.weblead" method="post">
First Name: <input type=text name="FirstName" size="25"><br>
Last Name: <input type=text name="LastName" size="25"><br>
<input type=submit value="Submit Form">
</form>

It works great except it doesn't validate for a REAL email address. How to alter this form so that it does?

The script can't contain any dollar symbols otherwise Tomcat (my server environment) crashes.

Jake
  • 61
  • 1
  • 1
  • 2
  • 11
    You should fix your server. – SLaks Jun 21 '10 at 17:47
  • 1
    duplicate: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript http://stackoverflow.com/questions/2783672/email-validation-javascript http://stackoverflow.com/questions/940577/javascript-regular-expression-email-validation – Mauricio Scheffer Jun 21 '10 at 17:49
  • 1
    Tomcat can definitely serve scripts containing "$" characters. Also it's hard to verify *syntax* for an email address, and *impossible* to tell whether it's real. – Pointy Jun 21 '10 at 17:51

3 Answers3

16

You can use regex in javascript

function is_email(email){      
var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailReg.test(email); } 

http://richwd.com/email-javascript

rich code
  • 169
  • 1
  • 3
1

I would suggest you use jQuery and the Validation plug-in:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

It's flexible, reliable and easy to use.

Keith Adler
  • 20,880
  • 28
  • 119
  • 189
0

This is a quick and dirty solution. Modify the value of the emailRegexp variable to suit your needs. There are already some examples here. As Pointy pointed out, it's generally better to have false positives than false negatives.

function formCheck(formobj){
    var fieldRequired = Array("Name", "Email", "Phone", "comments");
    var fieldDescription = Array("Name", "Email", "Phone", "Comments");
    var alertMsg = "Please complete the following fields:\n";
    var emailRegexp =/@/;

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "text":
            case "textarea":
                if ( obj.value == "" || 
                     obj.value == null || 
                     ( fieldRequired[i] == "Email" && !obj.value.match(emailRegexp) ))
                {

                    alertMsg += " - " + fieldDescription[i] + "\n";
                }


                break;
            default:
            }
            if (obj.type == undefined){
                var blnchecked = false;
                for (var j = 0; j < obj.length; j++){
                    if (obj[j].checked){
                        blnchecked = true;
                    }
                }
                if (!blnchecked){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
}
wnrph
  • 3,293
  • 4
  • 26
  • 38