How to stop a validation function from executing after it encounters an error?

E

effendi

I wrote a very simple validation fucntion to check if all fields are
entered properly in a dialog box before returning this the values to
the parent form. The validation works finr however, I am not sure if I
am doing this correctly. For example if the telephone number field is
empty, the alert box appear telling the users to enter the telephone
number. However, once I click OK. the script appear to run and I get an
error message in the "dialog box window". What I would like to do is
let the user click ok and return back to the problem field. Below is my
code.

var thisdoc=document.forms[0]
var Role=getSelectedText(thisdoc.Role)
var MemberLookup=thisdoc.MemberLookup.value

if(Role==">>>Select Role") {
alert("Please select a role for this Team member")
thisdoc.Role.focus();
return
}

if (Role.substring(0,2)=="01" && thisdoc.TeamLeadAppt.value=="Y"){
alert("You can appoint only one person as Team Leader")
}

var ContactNo=thisdoc.ContactNumber.value

if(ContactNo.value==" ") {
alert("Please enter a Contact Number Team member")
thisdoc.ContactNumber.focus();
return;
}
if(ContactNo.length!=8) {
alert("Please enter a valid 8 digit telephone number this Team
member")
thisdoc.Role.focus();
return;
}
if(ContactNo.length!=8) {
alert("Please enter a valid 8 digit telephone number this Team
member")
thisdoc.Role.focus();
return;
}

window.opener.teamSelectionCallback(MemberLookup,Role,ContactNo)
window.close()
 
R

RobG

I wrote a very simple validation fucntion to check if all fields are
entered properly in a dialog box before returning this the values to
the parent form. The validation works finr however, I am not sure if I
am doing this correctly. For example if the telephone number field is
empty, the alert box appear telling the users to enter the telephone
number. However, once I click OK. the script appear to run and I get an
error message in the "dialog box window". What I would like to do is
let the user click ok and return back to the problem field. Below is my
code.

var thisdoc=document.forms[0]
var Role=getSelectedText(thisdoc.Role)
var MemberLookup=thisdoc.MemberLookup.value

if(Role==">>>Select Role") {
alert("Please select a role for this Team member")
thisdoc.Role.focus();
return
}

Taking a guess that you are running the validation using
onsubmit, then use:

return false;

and in your onsubmit:

onsubmit="return validationFun();"

to ensure the return value is passed to the form. That will stop
the form submitting when the validation fails. If all goes OK,
then just let the script end, successful completion will return
true.

[...]
 
R

Robert Mark Bram

Hello,

Are you trying to say that the script is entering more than one of your if
statements?

Is all of the code below in a single function?

Rob

:)
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 27 Feb 2005 02:33:58, seen in (e-mail address removed) posted :
if(ContactNo.length!=8) {
alert("Please enter a valid 8 digit telephone number this Team
member")

if (!/^\d{8}$/.test(ContactNo)) {
alert("Please enter a valid 8 digit telephone number for this Team member")

should be considerably better.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 28 Feb 2005
11:21:25, seen in Effendi Baba
Thanks for correcting the error message but what's does your test
function do? Does it check the number for valid characters only and that
it should only be 8 character long?

It tests for an 8 decimal digit string, to match the error message. It
does not check that the number is telephonically appropriate; it accepts
all of 00000000 to 99999999, and nothing else.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 2 Mar 2005
04:04:58, seen in Effendi Baba
<[email protected]> posted :

Please learn how to format news replies; start by reading the newsgroup
FAQ.
Thank you very much for this test. I think it will be useful in other
situation as well. Could you lead me to a resource that could help me
understand how to construct this test?

If what I have already given you is not enough, I can help no further.

How do you propose to transfer that reward to those who help you?
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top