Sathya Narayanan
Sathya Narayanan

Reputation: 3

disabling submit button till input fields are validated

i have disabled the submit button in my guestbook. it has 2 fields[name(textbox)&comment(textarea)].it has 2 other fields ID(primary key) and date.the function is:

function Frmvalidate() {
var nmchk=document.forms["guestform1"]["name"].value;
var cmntchk=document.forms["guestform1"]["comment"].value;
if (nmchk.length==0)
        {
        var namep = document.getElementById("namep");
        namep.innerHTML="name must be filled out";
        return false;
        }
else if (cmntchk.length==0)
        {
        var cmntp = document.getElementById("cmntp");
        cmntp.innerHTML="comment must be filled out";
        return false;
        }
else
    {
    document.getElementById("sbmt").disabled=false;
    return true;
    }

}

i have called the function in places: body tag's onload,button tag's onclick. still its not working and blank entries are being stored in my database.

Upvotes: 0

Views: 46

Answers (1)

Royi Namir
Royi Namir

Reputation: 148524

You dont need to disable the submit button

you gain noting from it. ( alerting the user , running another script etc...)

instead -- The submit button should stop its regular behaviour by this code :

<input type="submit" onclick="return Frmvalidate();"/>

meaning :

when you press the button , it will execute the function yielding True or False and if it's True (only) it will continue to the server.

Upvotes: 1

Related Questions