This demo will evaluate all non-valid entries in the field!
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>Test Page</title>
<style type="text/css">
/* <![CDATA[ */
html, body {
background-color : #f0f0f0;
border : none;
color : #696969;
font : normal normal normal 90%/1.6 Verdana, Tahoma, Helvetica, Arial, sans-serif;
height : auto;
min-height : 600px;
min-width : 800px;
text-align : center;
vertical-align : baseline;
width : auto; }
h2 {
background-color : #ccc;
border-bottom : 1px solid #708090;
border-top : 1px solid #708090;
font : 700 160% "Bernard MT Condensed";
line-height : 2ex;
margin-top : 0;
min-height : 2ex;
padding : .100em 0em .100em 1em;
white-space : nowrap; }
div {
border : none;
margin : 0%;
padding : 0%; }
div#main {
margin : 0% auto;
text-align : left;
height : 100%;
overflow : hidden;
width : 98%;
}
form {
background-color : #ccc;
color : #000;
padding : 1em;
border : 1px solid #000;
width : 30%;
}
label, input {
display : block; }
input {
margin-bottom : .500em;
}
div.tube {
border : 1px solid #ccc;
height : 600px;
margin : 1% auto 1% auto;
padding : 1.200em; }
/* ]]> */
</style>
<script type="text/javascript">
// <![CDATA[
var validate, showError, validEmail, nameANDpassword;
var username, password, eMail, error;
validEmail = /^(\w+[\.\-])*\w+@(\w+\.)+[a-zA-Z]+$/;
nameANDpassword = /^[a-zA-Z\d]{6,8}$/;
showErrors = function( error ) {
div = (( document.getElementById ) ? document.getElementById( "errors" ) : document.all.errors );
div.innerHTML = "";
eMSG = "<p>The following error" + (( error.length > 1 ) ? "s" : "" ) + " have occured:";
for ( x = 0; x < error.length; x++ ) {
getError = x + 1;
eMSG += "\n" + getError + ". " + error[ x ];
} div.innerHTML = eMSG + "</p>";
};
validate = function( form ) {
error = [];
username = form.uname.value;
password = form.pword.value;
eMail = form.email.value;
if ( !nameANDpassword.test( username )) {
error[ error.length ] = "Please enter a valid username.";
}
if ( !nameANDpassword.test( password )) {
error[ error.length ] = "Please enter a valid password.";
}
if ( !validEmail.test( eMail )) {
error[ error.length ] = "Please enter a valid email address.";
}
if ( error.length > 0 ) {
showErrors( error );
return false;
} return true;
};
// ]]>
</script>
</head>
<body>
<div id="main">
<div class="tube">
<h2>JavaScript Live DEMO!</h2>
<h3>Sample Form</h3>
<form id="frm" action="#" onsubmit="return validate( this );">
<div>
<label for="uname">username: <input type="text" id="uname" name="uname" value="" size="30" /></label>
<label for="pword">password: <input type="password" id="pword" name="pword" value="" size="30" /></label>
<label for="email">e-mail: <input type="text" id="email" name="email" value="" size="30" /></label>
<input type="submit" id="sbm" name="sbm" value="submit" />
</div>
<div id="errors"></div>
</form>
</div>
</div>
</body>
</html>