/    Sign up×
Community /Pin to ProfileBookmark

Simple Form validation – need help.

For some reason, the following code, doesn’t want to work.
It checks the first variable, but with out any connection for the value of the variable it prompts that there is a problem. Anyone knows why? and what should I do?
Thanks in advanced, Oz.

<html>
<head>
<script type=”text/javascript”>
function checkform()
{
if (name==””)
{
alert(‘There is a problem with the first field’);
return false;
}
else if (email==””)
{
alert(‘There is a problem with…’);
return false;
}
return true;
}
</script>

</head>
<body>

<form name=”feedback” action=”http://localhost/Test.asp” method=”post” onSubmit=”return checkform()”>
<input type=”text” name=”name”>
<input type=”text” name=”email”>
<input type=”submit” value=”next step”/>
</form>
<body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Nick_INov 30.2005 — It looks like you are comparing the object and not the value of the text.

A change on the if statement from name == '' to name.value == '' (or better yet, document.forms[0].name.value == '' will fix the comparison.
Copy linkTweet thisAlerts:
@redfoxNov 30.2005 — hope this solves your problem, ^^, i edited a few lines


<html>

<head>

<script type="text/javascript">

function checkform()

{

if (document.getElementByName("name").value=="")

{

alert("There is a problem with the first field");

return false;

}

else if (document.getElementByName("email").value=="")

{

alert("There is a problem with...");

return false;

}

return true;

}

</script>

</head>

<body>

<form name="feedback" action="http://localhost/Test.asp" method="post" onSubmit="return checkform()">

<input type="text" name="name">

<input type="text" name="email">

<input type="submit" value="next step"/>

</form>

<body>

</html>
Copy linkTweet thisAlerts:
@ozpo1authorDec 01.2005 — Work like a charm, thanks.
Copy linkTweet thisAlerts:
@ozpo1authorDec 01.2005 — the code u wrote me, didn't work for me. it moved to the next page, with out alerting anything. Thanks for your time.
Copy linkTweet thisAlerts:
@redfoxDec 01.2005 — can you elaborate more so i could help you out, through your explanation above, my understanding is only giving validation for the value given,

i have another remedy for that if you want , here it is

try this copy and paste and save it as sample.html, the page it will go to after all inputs proves to be valid will go to test.asp, and you can change the address you want it to go, but if my understanding to your question is wrong, please elaborate more and i'll help you


<html>

<head><title>sample.html</title>

<script type="text/javascript">

function checkform()

{

if(document.getElementById("name").value=="")

{

alert("There is a problem with the first field");

return false;

}

else if(document.getElementById("email").value=="")

{

alert("There is a problem with...second field");

return false;

}

else if((document.getElementById("name").value!="" && document.getElementById("email").value!=""))

{

alert("next step");

//change the content in window.location where you want the next step

window.location="Test.asp";

return false;

}

}

</script>

</head>

<body>

<form name="feedback" action="sample.html" method="post" onSubmit="return checkform();">

<input type="text" id="name">

<input type="text" id="email">

<input type="submit" value="next step"/>

</form>

<body>

</html>
×

Success!

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

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

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