HTML/JS Validation - javascript

How can I expand the javascript code so that the examination number can only have a maximum length of 4 digits entered and the user cannot type anymore. Also how can I expand the code so that the examination field isn't left out. Aswell for the radio buttons I want to add a validation so if someone checks any box e.g GCSE a message will come up saying you have ticked (radio button ticked)
Thanks in advance
<html>
<head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">
function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="name">Examination number</td>
<td><input type="text" name="Examination number" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
<p> Please select <b>ONE</b> button.</br>
<input type="radio" name="group1" value="GCSE">GCSE<br>
<input type="radio" name="group1" value="AS" checked>AS<br>
<input type="radio" name="group1" value="A2" checked>A2<br>
</body>
</html>
<html>
<head>
<title>Success message</title>
</head>
<body>
<H1> You entered all the data required </H1>
</body>
</html>

You can use maxlength:
<input type="text" id="test" name="test" maxlength="4"/>
or in JavaScript you can do this:
Example
var input = document.getElementById("test");
input.oninput = function(){
if (input.value.length > 4)
{
var str = input.value;
str = str.substring(0, str.length - 1);
input.value = str;
alert("Some error message here");
}
}

<input type="text" name="Examination number" maxlength="4"/> for maxlength
put this in form onsubmit="return validateForm();"
check this for javascript validation
http://www.w3schools.com/js/js_form_validation.asp
learn yourself.

Related

JSP page form validation: 2 forms and 2 scripts

I have two forms in my JSP page which should be validated by two embedded JS scripts (see code below). The trouble is that the scripts don't work properly and omit validating the first entries (username and loginName respectively). I had two external Javascript files to do the validation but that had a similar problem.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Scotia Login Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<!-- <script type="text/javascript" src="scripts/stafflogin.js"></script> -->
<!--<script type="text/javascript" src="scripts/userlogin.js"></script> -->
<link rel="stylesheet" type="text/css" href="styles/DWBA.css"/>
</head>
<body>
<div class="outer">
<div class="header"><h1>Scotia Island & Wildlife Cruises</h1></div>
<div class="box"><h2>Login Page</h2>
<script>
function validateform1() {
var username = document.stafflogin.username.value;
var password = document.stafflogin.password.value;
if (username === null || username === "") {
alert("Please enter the admin username");
document.stafflogin.username.focus();
return false;
} else if (password === null || password === "") {
alert("Please enter the admin password");
document.stafflogin.password.focus();
return false;
}
}
</script>
<form name="stafflogin" action="CustomerServlet" onsubmit="return validateform1()"><!-- -->
<h3>Staff Login with username and password</h3>
<p> To login, enter details below:</p>
<table cellspacing="8" border="0">
<tr>
<td align="left"><p>Staff Username:</p></td><!-- USERNAME is admin -->
<td><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td align="left"><p>Staff Password:</p></td><!-- PASSWORD is admin -->
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td align="left"><input type="submit" value="stafflogin" name="submit" style="width:75px" /></td>
<td><br /><input type="hidden" name="username" value="" /></td>
</tr>
</table>
</form>
<br />
<script>
function validateform2() {
var name = document.userlogin.loginName.value;
var password = document.userlogin.loginPasswd.value;
if (name === null || name === "") {
alert("Please enter your username");
document.userlogin.loginName.focus();
return false;
} else if (password === null || password === "") {
alert("Please enter your password");
document.userlogin.loginPasswd.focus();
return false;
}
}
</script>
<form name="userlogin" method="POST" action="LoginServlet" onsubmit="return validateform2()">
<h3>Customer Login with login name and login password</h3>
<p> To login, enter details below:</p>
<table cellspacing="8" border="0">
<tr>
<td align="left"><p>Login name:</p></td><!-- -->
<td><input type="text" name="loginName" id="loginName"/></td>
</tr>
<tr>
<td align="left"><p>Login password:</p></td><!-- -->
<td><input type="password" name="loginPasswd" id="loginPasswd"/></td>
</tr>
<tr>
<td align="left"><input type="submit" value="userlogin" name="submit" style="width:75px" /><br /></td>
<td><input type="hidden" name="loginName" value="" /></td>
</tr>
</table>
</form>
<br />
<form name="Form" action="CustomerServlet">
<h3>New Customer registration</h3>
<p>To register, click on "register" below:</p>
<table cellspacing="8" border="0">
<tr>
<td><input type="submit" value="register" name="submit" style="width:75px" /></td>
</tr>
</table>
</form>
</div>
<div class="footer"><div align="center"><!-- ALIGNS EVERYTHING WITHIN DIV -->
<p>© Copyright Scotia Island & Wildlife Cruises, Harbour Road, Morar, Invernessshire.</p>
<form>
<tr>
<td><input type="submit" value="home" name="submit" style="width:75px" /></td>
</tr><br />
</form></div>
</div><br />
</div>
</body>
</html>
As I am a novice at Javascript I need some help to work out why two separate forms and validation scripts won't work as expected. (I have other JSP pages with single external Javascript validation scripts and they all work as intended).
Can anyone suggest a solution?
The answer is to remove the hidden inputs from each form eg.
<input type="hidden" name="username" value="" />
and
<input type="hidden" name="loginName" value="" />
so the forms look like this:
<script type='text/javascript'>
function validateform1() {
'use strict';
var username = document.stafflogin.username.value;
var password = document.stafflogin.password.value;
if (username === null || username === "") {
alert("Please enter the admin username");
document.stafflogin.username.focus();
return false;
} else if (password === null || password === "") {
alert("Please enter the admin password");
document.stafflogin.password.focus();
return false;
} else {
document.stafflogin.submit();
}
}
</script>
<form name="stafflogin" action="CustomerServlet" onsubmit="return validateform1()">
<h3>Staff Login with username and password</h3>
<p> To login, enter details below:</p>
<table cellspacing="8" border="0">
<tr>
<td align="left"><p>Staff Username:</p></td>
<td><input type="text" name="username" id="username" required /></td>
</tr>
<tr>
<td align="left"><p>Staff Password:</p></td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td align="left"><input type="submit" value="stafflogin" name="submit" style="width:75px" /></td>
</tr>
</table>
</form>
and this:
<script type='text/javascript'>
function validateform2() {
'use strict';
var name = document.userlogin.loginName.value;
var password = document.userlogin.loginPasswd.value;
if (name === null || name === "") {
alert("Please enter your username");
document.userlogin.loginName.focus();
return false;
} else if (password === null || password === "") {
alert("Please enter your password");
document.userlogin.loginPasswd.focus();
return false;
} else {
document.userlogin.submit();
}
}
</script>
<form name="userlogin" method="POST" action="LoginServlet" onsubmit="return validateform2()">
<h3>Customer Login with login name and login password</h3>
<p> To login, enter details below:</p>
<table cellspacing="8" border="0">
<tr>
<td align="left"><p>Login name:</p></td>
<td><input type="text" name="loginName" id="loginName"/></td>
</tr>
<tr>
<td align="left"><p>Login password:</p></td>
<td><input type="password" name="loginPasswd" id="loginPasswd"/></td>
</tr>
<tr>
<td align="left"><input type="submit" value="userlogin" name="submit" style="width:75px" /><br /></td>
</tr>
</table>
</form>
In short there was nothing wrong with the Javascript.

Validating Information Has Been Input

I was just wondering if anyone was able to help me with a JavaScript validation routine, Im trying to make it so that the Exam ID Number gets validated after information has been submit, So far all that is needed is name and subject and it will allow the examiner to proceed through without entering the exam ID number, Can anyone help me with this.
<script language="javascript"" type="text/javascript">
function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter your Exam ID Number \n";
document.ExamEntry.subject.focus();
document.getElementById('Exam Number').style.color="red";
result = false;
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
<style type="text/css">
h1,h2,h3,h4,h5,h6 {
font-family: "Comic Sans MS";
}
</style>
<h1>Exam Entry Form</h1>
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr> <tr>
<td id="Exam Number">Exam ID Number</td>
<td><input type="Number" name="ID Number"maxlength="4" > </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
Here is the full html
<html>
<head>
<script type="text/javascript">
function validateForm() {
var result = true;
var msg = "";
if (document.getElementById("txtname").value == "") {
msg += "You must enter your name \n";
document.getElementById("name").focus();
document.getElementById('name').style.color = "red";
result = false;
}
if (document.getElementById("txtsubject").value == "") {
msg += "You must enter the subject \n";
document.getElementById("subject").focus();
document.getElementById('subject').style.color = "red";
result = false;
}
if (document.getElementById("ID_Number").value == "") {
msg += "You must enter your Exam ID Number \n";
document.getElementById("ID_Number").focus();
document.getElementById('Exam Number').style.color = "red";
result = false;
}
if (msg == "") {
return result;
}
else {
alert(msg)
return result;
}
}
</script>
</head>
<body>
<form id="ExamEntry">
<h1>
Exam Entry Form</h1>
<table width="50%" border="0">
<tr>
<td id="name">
Name
</td>
<td>
<input type="text" id="txtname" />
</td>
</tr>
<tr>
<td id="subject">
Subject
</td>
<td>
<input type="text" id="txtsubject" />
</td>
</tr>
<tr>
<td id="Exam Number">
Exam ID Number
</td>
<td>
<input type="Number" id="ID_Number" maxlength="4">
</td>
</tr>
<tr>
<td>
<input type="submit" name="Submit" value="Submit" onclick="return validateForm();" />
</td>
<td>
<input type="reset" name="Reset" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>

Javascript alert not fired

For some reason my java script is changing the colour of the text but not showing my error message, any help would be greatful!
The code is a simple HTML form that asks for examiners name, number, subject and also has some radio buttons
<HTML>
<head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">
function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.ExaminationNumber.value=="") {
msg+="You must enter the Examination Number \n";
document.ExamEntry.ExaminationNumber.focus();
document.getElementById('Examination Number').style.color="red";
result = false;
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="S:\codes\success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<title>Exam entry</title>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="Examination Number">ExaminationNumber</td>
<td><input type="text" name="Examination Number" /></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<tr>
<td><input type="reset" name="Reset" value="Reset" /></td>
<input type="radio" name="qualification" value="GCSE">GCSE<br>
<input type="radio" name="qualification" value="AS">AS<br>
<input type="radio" name="qualification" value="A2">A2<br>
</form>
</tr>
</table>
</body>
</HTML>
IDs cannot have spaces.. Change Examination Number to something like Examination_Number or ExaminationNumber
Fixed jsFiddle example

Validation password not working

I am working on sign up form using html form , but i am facing problem my password cannot validate when I enter wrong password and enter write past does not show anything so what should i do any help.
function validate(){
var = pass(document.getElementById("pwd").value;
var = pas1(document.getElementById("cpwd").value;
if (pass==pas1)
{
}
else
{
alert("Try again");
}
}
</script>
</head>
<body>
<p class="style2 style8">SIGN UP FORM!!!!</p>
<form onclick="validate()" action="insert.php" method="post">
<table width="270" height="386" border="1" align="right" bordercolor="#993366" bgcolor="#CCCCCC"><td width="260"><table width="232" border="1">
<tr>
<td colspan="2"><span class="style2">loginform</span></td>
</tr>
<tr>
<td width="72"><span class="style5">Name</span></td>
<td width="144"><label>
<input name="name" type="text" id="name" onblur="MM_validateForm('name','','R');return document.MM_returnValue" placeholder="Name" />
</label></td>
</tr>
<tr>
<td width="72"><span class="style5">Gender</span></td>
<td><p>
<label>
<input type="radio" name="gender" value="Male" id="gender"/>
<span class="style5">Male</span></label>
<span class="style5" onfocus="MM_validateForm('fname','','R');return document.MM_returnValue"><br />
<label>
<input type="radio" name="gender" value="female" id="gender" />
female</label>
</span><br />
</p></td>
</tr>
<td width="72"><span class="style5">DOB</span></td>
<td width="144"><span id="sprytextfield1">
<label>
<input name="dob" type="text" id="dob" onblur="MM_validateForm('dob','','R');MM_validateForm('dob','','R');return document.MM_returnValue" placeholder="yyyy/mm/dd"/>
</label>
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
</tr>
<tr>
<td><span class="style5">Password</span></td>
<td><label>
<input name="pwd" type="password" id="pwd" onchange="MM_validateForm('pwd','','R');return document.MM_returnValue" placeholder="password"/>
</label></td>
</tr>
<tr>
<td width="72"><span class="style5">Confirm Password</span></td>
<td width="144"><label>
<input name="cpwd" type="password" id="cpwd" onchange="MM_validateForm('cpwd','','R');return document.MM_returnValue" placeholder="Confirm Password"/>
</label> </td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="submit" id="submit" value="Signup" />
</div>
</label></td>
</tr>
</table>
<label></label></td>
</tr>
</table>
</form>
<h1> </h1>
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"yyyy/mm/dd"});
//-->
</script>
</body>
</html>
there is no pass in your content. You are writing it as:
var = pass(document.getElementById("pwd").value;
var = pas1(document.getElementById("cpwd").value;
Which should be this:
var pass = (document.getElementById("pwd").value;
var pas1 = (document.getElementById("cpwd").value;
Because the variable's name comes before the = sign. To show that this variable is having this value!
So, now lets use this one and try it like this:
if(pass==pas1) {
// save the password or whatever
} else {
// please try again..
}
This way, the user will check the inputs again. And it they are not working. Then you must first check that whether you are using correct ID of the element, or you are using ClassName as ID!
That's what the issue might be.
Well, for starters,
var = pass(document.getElementById("pwd").value;
var = pas1(document.getElementById("cpwd").value;
should be
var pass = document.getElementById("pwd").value;
var pas1 = document.getElementById("cpwd").value;
Edit:
Also, change onclick="validate()" to onclick="return validate();" and add return false; after the alert("Try again");.
Just small change in your code
function validate(){
var pass = document.getElementById("pwd").value;
var pas1 = document.getElementById("cpwd").value;
if (pass==pas1){
}
else{
alert("Try again");
return false;
}
}

Making validation for both text field and checkbox

Please can I ask for help with my form. I am wanting to make both the text fields and check box required fields. The check box is validating, all I need now is for the text fields to require validation. I'm using javascript.
HTML:
<form name="form" method="post" action="result.php" onSubmit="return checkme()">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Name: </td>
<td> <input name="Name" type="text" id="Name" size="20"></td>
</tr>
<tr>
<td>Email: </td>
<td> <input name="Email" type="text" id="Email" size="20"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="checkbox" name="agree" value="agree_terms"> I agree to the terms</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Clear"></td>
</tr>
</table>
</form>
Javascript:
<script language="JavaScript" type="text/JavaScript">
<!--//hide script
function checkme() {
missinginfo = "";
if (!document.form.agree.checked) {
missinginfo += "\n - You must agree to the terms";
}
if (missinginfo != "") {
missinginfo ="__________________________________\n" +
"Required information is missing: \n" +
missinginfo + "\n__________________________________" +
"\nPlease complete and resubmit.";
alert(missinginfo);
return false;
}
else {
return true;
}
}
</script>
Add this JS code for validating the text field
if(document.form.Name.value==""){
missinginfo += "Required field";
}
Same for e-mail field also.
If you convert you doctype to <!DOCTYPE html> then you can use
required="true"
It will be implemented as follow
<input name="Name" type="text" id="Name" size="20" required="true">
Most browsers support HTML5. Alternatively make use of jQuery validation which is fairly simple to use.

Categories

Resources