views:

74

answers:

3

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.

A: 

Use this Regex (suggested by Pointy): http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html I didn't include it in the function below to spare everyone else from having their browser window filled up.

Try this function:

function validEmail(input)
{
    var emailRegex = /*REGEX GOES HERE/i;
    return (emailRegex.test(input));
}
SimpleCoder
There are TLDs that are more than 4 characters long.
SLaks
Bad idea. If a site's form doesn't allow me to type my address in a way that I know to be valid, I'm going to become irate. Why make your customers irate?
Pointy
Also that website seems to have been written by a fool, so I suggest trusting little or nothing you read there.
Pointy
Well use whatever Regex you want. I'm not going to hunt down the perfect one for you, I'm just providing a place to start.
SimpleCoder
Here you go, @SimpleCoder: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html :-) :-) :-)
Pointy
@Pointy, that will work although it may be more than @Jake needs. Anyway, I've edited my post to reflect your suggestion. Thanks!
SimpleCoder
A: 

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.

Nissan Fan
A: 

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;
    }
}
artistoex