...

View Full Version : Javascript Validation Problem



m@m
07-25-2011, 10:13 AM
I use this javascript validation in my form..
http://www.tizag.com/javascriptT/javascriptform.php

but without validation values go to database.. Can anyone guide me how to solve this?

First i import javascript file like this..

<script type="text/javascript" src="validate.js"></script>

Part of the My code goes like this....


<form action="Process_reg.php" method="post" name="reg" id="reg" onsubmit="return formValidator()">
<div id="accordion" style="width:400px; height:500px; font-size:12px;
font-family:Arial, Helvetica, sans-serif">




<h3><a href="#">General Details</a></h3>

<div>

<table cellpadding="5px" cellspacing="5px">
<tr>
<td>First name</td>
<td><input type="text" name="fname" id="fname" size="20px" title="Must be at least 8 characters."
/></td>
</tr>

I just only try o validate email field only... email field goes like this

<tr>
<td>Email</td>
<td><input type="text" name="email" id="email" size="20px" title="Enter Valid Email Address"
/></td>
</tr>


My whole code attach here...
Click here to download (http://www.sendspace.com/file/e2sry0)

Thanks

chump2877
07-25-2011, 11:01 AM
You are including a lot of javascript files in your PHP file, so there could be a conflict between those files and validate.js.

What exactly is in validate.js?...Can you paste the code here?

Also, before we go any further, can you tell me if you are receiving any JS errors in the error console?

One final thing: Are you supplementing your client side validation with server side validation? I kind of got the impression that you aren't. You'll want to add server side validation as well, because JS validation can be disabled in the browser.

m@m
07-25-2011, 11:22 AM
Not getting any error in the console....

here goes the .js code.........


<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var firstname = document.getElementById('firstname');
var addr = document.getElementById('addr');
var zip = document.getElementById('zip');
var state = document.getElementById('state');
var username = document.getElementById('username');
var email = document.getElementById('email');

// Check each input in the order that it appears in the form!
if(isAlphabet(firstname, "Please enter only letters for your name")){
if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
if(isNumeric(zip, "Please enter a valid zip code")){
if(madeSelection(state, "Please Choose a State")){
if(lengthRestriction(username, 6, 8)){
if(emailValidator(email, "Please enter a valid email address")){
return true;
}
}
}
}
}
}


return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>

chump2877
07-26-2011, 08:23 AM
The first thing I would do is remove the following markup from validate.js:


<script type='text/javascript'>

...and...


</script>

HTML markup cannot "live" inside of a file with a .js extension. So remove the markup and see if that helps...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum