Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic JavaScript Form Validation

Status
Not open for further replies.

carlg

Programmer
Joined
Jun 23, 2004
Messages
88
Location
US
I want to validate an HTML form using Javascript.

Here is what I am doing

User clicks submit button for form
Data entered is bad, javascript sends out an alert box to say bad data, but how do I get the form to not get submitted?

I'm using the onClick event for the submit button.

My javascript looks something like this

function verify()
{
if (baddata)
{
alert "bad data"
}}
 
Here you go
Code:
<script type="text/javascript">
<!--//
function allowSubmit() {

	/* do something here */

	if (bad) {
		d.allow.disabled = true;
	}
}
//-->
</script>

<form onSubmit="allowSubmit()">
	<input type="submit" name="allow" value="Submit">
</form>

M. Brooks
 
Seems like it still submits my form

Here's the JS


function abc(){
document.empadd.addbutton.disabled=true;
alert ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}



Here's the form


<FORM NAME="empadd" METHOD="POST" onSubmit="abc()">
<INPUT NAME="addbutton" TYPE="submit" VALUE="Submit">
</FORM>
 
Got it

Here is what I did


function abc(){
alert ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
return false;
}



Here's the form


<FORM NAME="empadd" METHOD="POST" onSubmit="return abc()">
<INPUT NAME="addbutton" TYPE="submit" VALUE="Submit">
</FORM>
 
Code:
<script type="text/javascript">
<!--//
function allowSubmit() {

    /* do something here */

    var d = document.empadd;

    if (good) {
        d.submit();
    }
    else {
        d.addbutton.disabled = true;
    }
}
//-->
</script>

<FORM NAME="empadd" METHOD="POST">
        <INPUT NAME="addbutton" TYPE="button" VALUE="Submit" onClick="allowSubmit()">
</FORM>

M. Brooks
 
Ah, now i see

Looks like there's 2 different ways.


Thanks for the help!!!

Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top