/    Sign up×
Community /Pin to ProfileBookmark

Validating 3 text boxes – to ensure they aren’t empty

I am trying to get a basic validation working which checks all three fields are not empty- so far I can only get it to check the first one….

Here is my code – hope someone can help me to crack it!

<html>
<head>
<title>Page title</title>
<script language=”JavaScript”>
<!–
function checkform()
{
if (custform.custname.value==””)
{
alert (“No Data entered for Customer Name”);
return false;
}
}

{
if (custform.custemail.value==””)
{
alert (“No Data entered for Customer Email”);
return false;
}
}

{
if (custform.custccno.value==””)
{
alert (“No Data entered for Customer Credit Number”);
return false;
}
}

return true;
</script>

</head>
<body MARGINWIDTH=’0 MARGINHEIGHT=’0′ leftmargin=’0′ topmargin=’0′>
<FORM name=”custform” action=”formcheck1.html” onsubmit=”return checkform()” method=”post”>
<table border=’0′ cellspacing=’0′ cellpadding=’0′ width=’100%’>

<tr>
<td>Customer Name</td>
<td><INPUT TYPE=’text’ size=’50’ name=’custname’></td>
</tr>
<tr>
<td>E mail Address</td>
<td><INPUT TYPE=’text’ size=’50’ name=’custemail’></td>
</tr>
<tr>
<td>Credit Card Number</td>
<td><INPUT TYPE=’text’ size=’50’ name=’custccno’></td>
</tr>
<TR>
<TD><input type=’SUBMIT’ value=’click here!!’></TD>
<TD>&nbsp;</TD>
</TR>

</table>
</FORM>

</body>
</html>

Ideally I’d like to have a more advanced validation than just checking for any input – typically checking that the email has a “@” and a “.” and checking that the credit card has a numerical values and is 16 digits….

If anyone knows of any code to do this, I’d appreciate it…..

Many Thanks!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KeveyNov 01.2005 — Here's a script that validates 3 fields and puts out an alert message if they fail to fill in a field, and tells them which field they missed. You'll have to tweak it to work for you.

[CODE]<SCRIPT type="text/javascript">
function validate(frm) {
var inputFields = new Array("name" ,"city" ,"state");
var counter;
var name;
var msg = "Please complete the following fields:n";
var badFields = "";
for (counter = 0; counter < inputFields.length; counter++) {
name = inputFields[counter];
if (frm.elements[name].value.length == 0) {
if (name == "formmail_mail_email") {
badFields = badFields + " - n";
} else {
badFields = badFields + " - " + name + "n";
}
}
}
if (badFields.length != 0) {
alert(msg + badFields);
return false;
}
return true;
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@avaNov 01.2005 — How about this for a function...
[CODE]function checkform()
{
var myreturn = true;

if (custform.custname.value=="")
{
alert ("No Data entered for Customer Name");
myreturn=false;
}

if (custform.custemail.value=="")
{
alert ("No Data entered for Customer Email");
myreturn=false;
}

if (custform.custccno.value=="")
{
alert ("No Data entered for Customer Credit Number");
myreturn=false;
}

return myreturn;
}[/CODE]
×

Success!

Help @madman070578 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.28,
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,
)...