Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation help

Status
Not open for further replies.

lostnewbie

Programmer
Nov 6, 2002
5
US
hey I'm totally clueless when it comes to javascript. I made a "joke" quiz for my friends to take and I want to score it but I don't know how. I try looking online for help but a lot of the help is for radio and checkbox forms. My form is text so I don't know how to make it work. Please help me. This is what I got so far:
<html>
<head>
</head>

<body bgcolor=&quot;#FFFFFF&quot;>

<form name=&quot;Do you know me&quot;>

<h1>Please answer the question below</h1>

<p>My Name: <input type=&quot;text&quot; name=&quot;myname&quot;></p>
<p>My Last Name: <input type=&quot;text&quot; name=&quot;lastname&quot;></p>
<p>My birthday: <input type=&quot;text&quot; name=&quot;bday&quot;></p>
<p>My favorite color: <input type=&quot;text&quot; name=&quot;color&quot;></p>
<p>My favorite food: <input type=&quot;text&quot; name=&quot;food&quot;></p>
<p>My pet name: <input type=&quot;text&quot; name=&quot;pet&quot;></p>
<p>My crush's name: <input type=&quot;text&quot; name=&quot;crush&quot;></p>



<p><input type=&quot;submit&quot; name=&quot;send&quot; value=&quot;Send Details&quot;></p>

</form>

</body>

</html>

 
so what is it you actually want to validate? empty space? a actual value? can you be a little more specific.
if you're validating a certain value then just do
don't put spaces in the form name <form name=&quot;Doyouknowme&quot;>

function val(){
if (!(Doyouknowme.myname.value == &quot;lostnewbie&quot;)) {
alert(&quot;you lose!&quot;);
return false;
}

} A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Well, is the value what goes in the text box and if they type in the wrong value then they will get the alert box that they lose? I also want to score my test so for every question that they got right they get one pt and for every question they got wrong they get no points, and the bottom of the test there will be a score total
 
Validating a 100% text based questionairre is pretty difficult, since typos, abbreviations, etc will make everything except the exact answers you check for 'wrong'.

Basically, your form header must have an onsubmit=&quot;return function()&quot; command in it. Replace the &quot;function()&quot; with your own function name.

Then write your javascript function, looking for certain values in each form field to validate.
 
seeing as you a validating for certain values this is a very easy task. You will want to convert to lower case though to take that factor out. I have made this as easy as I could to show you. There are far easier and quicker not to mention shorter ways to do this but this will show you exactly what is going on in the validation step ect.. If you want to make this better then you should use a array with the correct answers in it and simple validate once for all of the fields checking against the array values. That would take a if statement for each field and create one for the entire form. This will be better for starting off I think though. Try it. any questions let me know.

like I said, this way is very simple and straight forward but long winded in length when it really doesn't have to be.
<html>
<head>
<script language=&quot;javascript&quot;>
var total = 0;
function val(){
if (!(Doyouknowme.myname.value.toLowerCase() == &quot;lostnewbie&quot;)) { //convert to lower case and validate answer
Doyouknowme.myname.value = &quot;my name is lostnewbie&quot;; //auto fill correct answer
Doyouknowme.myname.focus(); //set focus to wrong answer
alert(&quot;you lose!&quot;); //tell them if they lost
Doyouknowme.total.value = total; // show score
}
else {
total += 1; // add a point
Doyouknowme.total.value = total; // show score
}

if (!(Doyouknowme.lastname.value.toLowerCase() == &quot;lostnewbie&quot;)) { //convert to lower case and validate answer
Doyouknowme.lastname.value = &quot;my name is lostnewbie&quot;; //auto fill correct answer
Doyouknowme.lastname.focus(); //set focus to wrong answer
alert(&quot;you lose!&quot;); //tell them if they lost
Doyouknowme.total.value = total; // show score
}
else {
total += 1; // add a point
Doyouknowme.total.value = total; // show score
}
}
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<form name=&quot;Doyouknowme&quot;>
<h1>Please answer the question below</h1>
<p>My Name: <input type=&quot;text&quot; name=&quot;myname&quot;></p>
<p>My Last Name: <input type=&quot;text&quot; name=&quot;lastname&quot;></p>
<p>My birthday: <input type=&quot;text&quot; name=&quot;bday&quot;></p>
<p>My favorite color: <input type=&quot;text&quot; name=&quot;color&quot;></p>
<p>My favorite food: <input type=&quot;text&quot; name=&quot;food&quot;></p>
<p>My pet name: <input type=&quot;text&quot; name=&quot;pet&quot;></p>
<p>My crush's name: <input type=&quot;text&quot; name=&quot;crush&quot;></p>
<p>Total score so far: <input type=&quot;text&quot; name=&quot;total&quot;></p>

<p><input type=&quot;button&quot; name=&quot;send&quot; value=&quot;Send Details&quot; onClick=&quot;val()&quot;></p>
</form>
</body>
</html>
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top