/    Sign up×
Community /Pin to ProfileBookmark

Validating Fields so they are not null

Hi, I’m trying to validate the fields on my form to make sure that something is entered in each field, I’m not getting picky yet, I just want to make sure they have entered something in each field. For some reason, it isn’t working how I thought it should. Below is my code. If anyone has any suggestions or ideas, I’d greatly appreciate it! Thanks!!!

<script>
function complete()
{

var fname=(document.Payment.Name.value);
var addr=(document.Payment.Address.value);
var city=(document.Payment.City.value);
var state=(document.Payment.State.value);
var zip=(document.Payment.Zip.value);
var phone=(document.Payment.Phone.value);
var DOB=(document.Payment.DOB.value);
var cc=(document.Payment.CC.value);
var ccNum=(document.Payment.CCNum.value);
var month=(document.Payment.ExpMonth.value);
var year=(document.Payment.ExpYear.value);

if(fname ==””)
alert(“hello”);
alert(“Please enter your name.”);
document.Payment.Name.focus();

else
document.Payment.Address.focus();

}

</script>

<FORM name=Payment action=”” method=post>
<TABLE>
<TBODY>
<TR>
<TD><B>Full Name:</B></TD>
<TD><INPUT name=Name></TD></TR>
<TR>
<TD><B>Address:</B></TD>
<TD><INPUT name=Address></TD></TR>
<TR>
<TD><B>City:</B></TD>
<TD><INPUT name=City></TD></TR>
</table>
</form>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorApr 07.2004 — You forget to use an event handler (onclick, onsubmit, on[i]something[/i]) to fire the function.

The best way is to use onsubmit placed in form tag, restricted by a return false/true method.

To shorten the code if many text fields, better use an incremental statement.

Try this:
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
function verify(){
d=document.forms[0].elements;
for (i=0;i<d.length;i++){
if(d[i].type == 'text'){
if(d[i].value == ''){
alert('You forgot to complete a text field');
d[i].focus();
return false
}
}
}
return true
}
</script>
</head>
<body>
<form onsubmit="return verify()">
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<input type="text"><br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
[/code]
×

Success!

Help @jguldseth 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 5.21,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...