Validation

B

Betty

I have user profile form that i wish to valadate as the user clicks the
submit button.
I have started to try and validate the first couple of fields to see if
this works but it doesnt?

This is the code that i have in the head section...
<script language="JavaScript">

function validateForm()
{
alertText = 'You must complete the required fields';
returnValue = true;

if (document.MobileProfile.UserName == '')
{
alertText = alertText + ' User Name \n';
returnValue = false;
}

if (document.MobileProfile.Department.value == '')
{
alertText = alertText + ' Department \n';
returnValue = false;
}

if (returnValue == false)
{
alert([alertText]);
}

return returnValue;

}
</script>

This is the code that i have in the Form section...
<form method="POST" action="--WEBBOT-SELF--" name="myForm"
onsubmit="validateForm()">


I have also been advised to remove the onsubmit section in the <form>
and to add the following within the codse for the submit button...
<input type="submit" value="Submit" Name="submit" OnClick="javascript:
validateForm();">

Either way i cant seem to get this to work.

Can anyone help?


Kind Regards
Betty
 
E

Erwin Moller

Betty said:
I have user profile form that i wish to valadate as the user clicks the
submit button.
I have started to try and validate the first couple of fields to see if
this works but it doesnt?

This is the code that i have in the head section...
<script language="JavaScript">

function validateForm()
{
alertText = 'You must complete the required fields';
returnValue = true;

if (document.MobileProfile.UserName == '')
{
alertText = alertText + ' User Name \n';
returnValue = false;
}

if (document.MobileProfile.Department.value == '')
{
alertText = alertText + ' Department \n';
returnValue = false;
}

if (returnValue == false)
{
alert([alertText]);
}

return returnValue;

}
</script>


Hi,

Your way of addressing the formelements is incomplete.
Try this:
document.forms["myForm"].MobileProfile.Department.value

This is the code that i have in the Form section...
<form method="POST" action="--WEBBOT-SELF--" name="myForm"
onsubmit="validateForm()">


I have also been advised to remove the onsubmit section in the <form>
and to add the following within the codse for the submit button...
<input type="submit" value="Submit" Name="submit" OnClick="javascript:
validateForm();">

Either way i cant seem to get this to work.

Can anyone help?

Both ways work, but using onSubmit() has a great advantage: If a client has
Javascript disabled, the form will still work.
Of course the formchecking will not work if JS is disabled.

If you use the onClick, and change the type from "submit" to "button", only
JS-enabled browsers will post the form (after validation).

So make sure your serverside script will check the form again for
empty/naughty fields. Do that ALWAYS.


Good luck

Regards,
Erwin Moller
 
R

Richard Cornford

Betty said:
I have user profile form that i wish to valadate as the user clicks
the submit button. I have started to try and validate the first couple
of fields to see if this works but it doesnt?
This is the code that i have in the Form section...
<form method="POST" action="--WEBBOT-SELF--" name="myForm"
onsubmit="validateForm()">

The - validateForm - function above is not the event handler, it is a
function call made by the event handling function. So returning true or
false from the - validateForm - function does not influence the
submitting of the form.

The value provided for an ONSUBMIT attribute is used, by the browser,
to create a function that is then the event handling function, and the
event handling function is the one that needs to be returning true or
false to influence the submitting of the form. The simplest way of
doing that is to make the function call into a return statement:-

onsubmit="return validateForm();"

- so that the actual event handler returns whatever value -
validateForm - returns.
I have also been advised to remove the onsubmit section in the
<form> and to add the following within the codse for the submit
button...
<input type="submit" value="Submit" Name="submit"
OnClick="javascript: validateForm();">

That would be *absolutely the worst thing you could do*. There are many
ways that the user may submit a form that do not involve a submit
button, and so would bypass such an event handler. While any user
action that directly triggers a form submit will result in the onsubmit
handler being called (at least if scripting is available at all on the
browser).

Also, do not put the character sequence 'javascript:' in intrinsic
event attributes (unless you are scripting for IE and have made
VBScritp the page default language) as doing so is useless (if
harmless) and wasteful.
Can anyone help?

Certainly whoever suggested the onclick handler for the submit button
can only do you harm.

Richard.
 
E

Erwin Moller

Hi,

Your way of addressing the formelements is incomplete.
Try this:
document.forms["myForm"].MobileProfile.Department.value

Now I went sloppy. :p

document.forms["myForm"].MobileProfile.Department.value
is most probably wrong too.
Which element do you want to address? MobilePhofile or Department?

It is hard to answer which one you need without knowledge of the form.

But, if you need a input type="text" or textarea formelement named
MobileProfile, use this:
document.forms["myForm"].MobileProfile.value

Regards,
Erwin Moller
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 15 Sep 2006 02:25:28 remote, seen in
Betty <[email protected]>
posted :

ADDITIONAL comment :
<script language="JavaScript">

Deprecated : use said:
function validateForm()
{
alertText = 'You must complete the required fields';
returnValue = true;

if (document.MobileProfile.UserName == '')
{
alertText = alertText + ' User Name \n';
returnValue = false;
}

if (document.MobileProfile.Department.value == '')
{
alertText = alertText + ' Department \n';
returnValue = false;
}

IMHO, the \n would be better at the front of the strings.
if (returnValue == false)

No need to compare with a Boolean constant
{
alert([alertText]);
}

and I see no need to use an array there.

if ( ! returnValue ) alert(alertText)

will suffice.
return returnValue;

}
</script>

In News, indenting by two spaces is better than indenting with tab; any
adequate composing system will allow a global replace for that.


It's a good idea to read the newsgroup and its FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top