Java scripts to Validate the Form

This is a simple javascript to validate our form’s page like register form or login form.

And this is the scripts . put this script in head :

<script language=”JavaScript”>
<!–
function validateForm()
{
var okSoFar=true
with (document.form)
{
if (name.value==”" && okSoFar)
{
okSoFar=false
alert(“Insert Your Name”)
name.focus()
}
if (address.value==”" && okSoFar)
{
okSoFar=false
alert(“Insert Your Address”)
address.focus()
}

if (password1.value !== password2.value && okSoFar)
{
okSoFar=false
alert(“Password do not match, please repeat”)
password2.focus()
}

if (okSoFar==true)  submit();
}
}
// –>
</script>

After we put that script  we must create the form with 4  input form according the javascript, in this case we should create name,  address,  password1 and password2  input form.

And the last for submit button. we must insert   this script :  onclick=”javascript:validateForm()”

for ex ;

<input name=”submit” type=”submit”  onclick=”javascript:validateForm()” >

If  Done, when the user do not  type anythink word, the error message pop up will a rise.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s