/    Sign up×
Community /Pin to ProfileBookmark

Javascript Form Validation

I have some fields on a form. If one particular field (potentialvendorcomplete)equals the value “yes” AND another field (potentialvendor1) is blank, then i don’t want the form to be submitted and an alert message to appear.

However, using the logical code below, the alert comes up if potentialvendorcomplete = No. If potentialvendorcomplete = No then the alert should not come up! Any idea why this is not working?

[CODE]

function validateForm(form) { //This is the name of the function

if (form.potentialvendor1.value == “” && form.potentialvendorcomplete.value == “Yes”); { //This checks to make sure the field is not empty
alert(“You must enter a vendor to complete vendor sourcing”); //Informs user of empty field
form.potentialvendor1.focus( ); //This focuses the cursor on the empty field
return false; //This prevents the form from being submitted
}
//example
return true
}
</SCRIPT>

[/CODE]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameApr 21.2009 — About the only problem I see is the part in red - remove it and try:
if (form.potentialvendor1.value == "" && form.potentialvendorcomplete.value == "Yes")[COLOR="Red"];[/COLOR] { //This checks to make sure the field is not empty[/QUOTE]
Copy linkTweet thisAlerts:
@emzipoo4uauthorApr 21.2009 — Thank You! perfect.

I have another problem now. I'm doing the same kind of thing, but, I need to check whether a checkbox has been ticked or not. Any ideas ?

[CODE]
if ((form.meetingcompleted.value == "No") && (form.Phase1complete.checked == "True")) { //This checks to make sure the field is not empty
alert("You cannot complete phase 1 without holding an internal meeting"); //Informs user of empty field
form.meetingcompleted.focus( ); //This focuses the cursor on the empty field
return false; //This prevents the form from being submitted
} [/CODE]
Copy linkTweet thisAlerts:
@astupidnameApr 21.2009 — if ((form.meetingcompleted.value == "No") && (form.Phase1complete.checked == "True"))[/quote]
Change that to:
if (form.meetingcompleted.value == "No" &amp;&amp; form.Phase1complete.checked == true)
Or even:
if (form.meetingcompleted.value == "No" &amp;&amp; form.Phase1complete.checked)
Copy linkTweet thisAlerts:
@zygissimoApr 21.2009 — hi all,

i am not sure wheather i am asking in the right place, but i have a simple question:

is it possible to make a form with javascript where according to the radio buttons checked the submit button directs user to specific url. for example:

Cars:

(radio button) BMW

(radio button) Lexus

(radio button) Audi

Colors

(radio button) Black

(radio button) Red

(radio button) Yello

(Submit)

so IF Lexus and Red are selected, the submit button takes user to red Lexus. Is it possible? and is javascript the right choise to do so?

thank you a lot for an answer
Copy linkTweet thisAlerts:
@astupidnameApr 21.2009 — is it possible to make a form with javascript where according to the radio buttons checked the submit button directs user to specific url[/quote]
The below will get you going on that:

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Some Title</title>
<script type="text/javascript">

function director(form) {
var inputs = form.getElementsByTagName('input'), sl, sr, i;
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type == 'radio' && inputs[i].checked) {
if (inputs[i].name == "brand") {
sl = inputs[i].value;
} else if (inputs[i].name == "color") {
sr = inputs[i].value;
}
}
if (sl && sr) {
form.action = sl + sr + '.html';
return true;
}
}
return false;
}

</script>
</head>
<body>
<div>
<form onsubmit="return director(this);" method="get" action="page2.php" name="carForm">
BRANDS:<br/>
Audi <input type="radio" name="brand" value="audi"><br/>
BMW <input type="radio" name="brand" value="bmw"><br/>
Lexus <input type="radio" name="brand" value="lexus"><br/>
<br/>COLORS:<br/>
Black <input type="radio" name="color" value="black"><br/>
Red <input type="radio" name="color" value="red"><br/>
Yellow <input type="radio" name="color" value="yellow"><br/>
<br/><input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@zygissimoApr 22.2009 — thanks a lot!

but still, if i need them to direct to the links i would manually write in? for example black lexus goes to http://www.lexus.com/lexus-is/blacks-lexus or whatever url that i need this combination to direct. Possible?

Thank you once again
Copy linkTweet thisAlerts:
@astupidnameApr 22.2009 — [code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Some Title</title>
<script type="text/javascript">

function director(form) {
var urls = {
audiblack: "http://www.audi.com/audi-is/blacks-audi",
audired: "http://www.audi.com/audi-is/red-audi",
audiyellow: "http://www.audi.com/audi-is/yellow-audi",
bmwblack: "http://www.bmw.com/bmw-is/blacks-bmw",
bmwred: "http://www.bmw.com/bmw-is/red-bmw",
bmwyellow: "http://www.bmw.com/bmw-is/yellow-bmw",
lexusblack: "http://www.lexus.com/lexus-is/blacks-lexus",
lexusred: "http://www.lexus.com/lexus-is/red-lexus",
lexusyellow: "http://www.lexus.com/lexus-is/yellow-lexus"
};
var inputs = form.getElementsByTagName('input'), sl, sr, i;
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type == 'radio' && inputs[i].checked) {
if (inputs[i].name == "brand") {
sl = inputs[i].value;
} else if (inputs[i].name == "color") {
sr = inputs[i].value;
}
}
if (sl && sr) {
form.action = urls[sl + sr];
return true;
}
}
return false;
}

</script>
</head>
<body>
<div>
<form onsubmit="return director(this);" method="get" action="page2.php" name="carForm">
BRANDS:<br>
Audi <input type="radio" name="brand" value="audi"><br>
BMW <input type="radio" name="brand" value="bmw"><br>
Lexus <input type="radio" name="brand" value="lexus"><br>
<br>COLORS:<br>
Black <input type="radio" name="color" value="black"><br>
Red <input type="radio" name="color" value="red"><br>
Yellow <input type="radio" name="color" value="yellow"><br>
<br><input type="submit" value="Submit">
</form>
</div>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@zygissimoApr 29.2009 — what should I do if I additionaly need a sum of choises in this form? I mean:

Cars:

(radio button) BMW 200$

(radio button) Lexus 300$

(radio button) Audi 400$

Colors

(radio button) Black 50$

(radio button) Red 60$

(radio button) Yello 70$

(Submit)

Sum of choices.

I need the sum to be displayed live, after each checked radio button. And when the sumbit button has been pushed, it takes to the url. This function is already working (thanks for that again).

So basecaly, I need two functions: one, that would work live, before Submiting, and the other one on Submit.

something like this, just way more simple if possible:

[url=http://store.apple.com/us/configure/MB466LL/A?mco=MTkzOTIzOQ]Example[/url]
Copy linkTweet thisAlerts:
@zygissimoMay 04.2009 — anyone could help please?
×

Success!

Help @emzipoo4u spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...