Jump to content

Form Validation


Fallen_angel

Recommended Posts

Hi I am hoping someone here can help me

I need to have 6 fields on my from as manditory , I have been able to add one more field so make it so two fields are checked , however once I try and do more than 2 it simply ignores me for some reason

this is what I started with [code]

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(field,"Report Title must be filled out!")==false)
  {report_title.focus();return false}
if (validate_required(field2,"Report Title must be filled out!")==false)
  {r_submitter.focus();return false}
}
}
[/code]

and as part of my form tag I have onsubmit="return validate_form(this)"  which makes it run when the form is submitted .  So far so good with one field  then I went and added a seccond required field like bellow

[code]

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(field,"Report Title must be filled out!")==false)
  {report_title.focus();return false}
else if (validate_required(field2,"Report Title must be filled out!")==false)
  {r_submitter.focus();return false}
}
}
[/code]

Still all worked and I was pretty happy , but then I went to add the rest of my required fields and thats when it just stopped listening to me ,

what I tried was

[code]
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(field1,"Report Title must be filled out!")==false)
  {field1.focus();return false}
else if (validate_required(field2"Report Title must be filled out!")==false)
  {field2.focus();return false}
else if (validate_required(field3"Report Title must be filled out!")==false)
  {field3.focus();return false}
}
}[/code]

What am I doing wrong ? is it where I am using else if ? should I be doing this a different way ?

Link to comment
https://forums.phpfreaks.com/topic/30210-form-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.