Seconds ago, it was working, and I did not change anything. My web app is a simple web-app. Javascript is responsible for form validation before it redirects it to the servlet. What did I do wrong?
Form validation is not validating. I am a novice programmer, so please help.
// form-validation (javascript)
function validateForm() {
var firstname = document.forms["myForm"]["förnamn"].value;
var lastname = document.forms["myForm"]["efternamn"].value;
var address = document.forms["myForm"]["address"].value;
var email = document.forms["myForm"]["email"].value;
var news = document.forms["myForm"]["news"].value;
if (firstname == null || firstname == "") {
alert("Var god och fyll i alla fält!");
return false;
} else if (lastname == null || lastname == "") {
alert("Var god och fyll i alla fält!");
return false;
} else if (address == null || address == "") {
alert("Var god och fyll i alla fält!");
return false;
} else if (email == null || email == "") {
alert("Var god och fyll i alla fält!");
return false;
} else if (email.indexOf("@hotmail.com") === -1) {
alert("Skriv in en korrekt mail-address. exempel@hotmail.com");
return false
}
}
//]]>
</script>
</head>
<body>
<fieldset style="width: 450px; padding: 20px">
<div id="page">
<div id="header">
<h1>Abdi Tem</h1>
<h2>Welcome to my website</h2>
</div>
<div id="bar">
<div class="link">
<a href="Hem.html">Hem</a>
</div>
<div class="link">
<a href="CV.html">INFO</a>
</div>
<div class="link">
<a href="Kontakt.html">Ḱontakt</a>
</div>
</div>
</div>
<h1>Hyr en programmerare:</h1>
<form name="myForm" action="ServletProjekt"
onsubmit="return validateForm()" method="post" id="myForm">
<table>
<tr>
<td>Ditt namn:</td>
<td><input type="text" name="fornamn" /></td>
</tr>
<tr>
<td>Ditt efternamn:</td>
<td><input type="text" name="efternamn" /></td>
</tr>
<tr>
<td>Din adress:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Din email:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Är ditt ärende akut?</td>
<td><input type="radio" name="howoften" value="often"
checked="checked" />JA <input type="radio" name="howoften"
value="not often" />NEJ</td>
</tr>
<tr>
<td>Vad vill du ha hjälp med?</td>
<td><input type="checkbox" name="news" value="Java"
checked="checked" />Programmering <input type="checkbox"
name="news" value="C" />Felsökning/Debugging</td>
</tr>
<tr>
<td>Hur många timmar kommer ditt ärende att ta?</td>
<td><select name="age">
<option>10-20h</option>
<option>30-40h</option>
<option>50-60h</option>
<option>70h+</option>
</select></td>
</tr>
</table>
<hr />
<input type="submit" name="submit" value="Beställ" />
</form>
</fieldset>
</body>
</html>
onsubmit function() in your form should return true.
Try putting return true after all conditions have been taken care for validations in
function validateForm()
See this: http://jsfiddle.net/phpadqcr/
I move your javascript code before the closing html tag, inside a <script></script> block and I found a typo here:
var firstname = document.forms["myForm"]["förnamn"].value;
You can fix it by replace the "förnamn" with "fornamn".
Two error in code
1) ["förnamn"]
: unicode character
2) Semi-column ( ;
)missing for last return statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.