[thelist] JS Form Validation

Chris Price chris.price at choctaw.co.uk
Thu Jul 1 13:12:30 CDT 2004


On 1/7/04 6:14 pm, "Joshua Hmielowski" <jhmielowski at comcast.net> wrote:

>> I have been trying to get the most simple validate form to work and
>> have had no luck.
>> I have tried MANY different ways of calling this JS function and to no
>> avail. For some reason
>> the function is not getting called.

I had terrible trouble today with the same thing. The trouble is I just kept
making stupid mistakes like missing off </script> and at one point couldn't
even see the page in the browser never mind get the js to work.

I did get it working and the code is brain-dead simple.

<form name="letmein" onsubmit="return validate()" action="url"
method="POST">

The JavaScript:

<script type="text/JavaScript">
<!--
function validate() {
    if (document.letmein.name.value == '') {
        alert('Please complete the Name field');
        return false; }
    else if (document.letmein.email.value == '') {
        alert('Please complete the Email field');
        return false; }
        else return true; }
// -->
</script>

The form requires both name and email to be completed.

I have onsubmit in all lowercase so that it validates XHTML1.0 strict.

Hope this helps

--
Chris price



More information about the thelist mailing list