Form Validation [*N0T WORKING*]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Form Validation [*N0T WORKING*]

Post by pHp_n0ob »

:banghead: My hardwork going into vein :banghead: dont know why its not working

Code: Select all

<?php include "../templates/header.php";?>
<script type='text/javascript'>
function formValidator(){// Setting variables
var name = document.getElementById('name');
var subject = document.getElementById('subject');
var comments = document.getElementById('comments');
var email = document.getElementById('email');// Check each input in the order that it appears in the form!
if(isAlphabet(name,"Please enter only letters for your name"))
{if(isAlphanumeric(subject,"Numbers and Letters Only for Subject"))
{
if(isAlphabet(comments,"Please provide a comment!"))
{
if(lengthRestriction(name, 3, 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)
}
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>
<form action="action.php" method="post" onSubmit="return formValidator">Name<br><input type="text" name="name" id="name"/>
<br>Email address<br><font color="red">*</font>[<a href="" onClick="vivek()">?</a>]<br><input type="text" name="email" id="email" />
<br>Subject<br><input type="text" name="subject" id="subject" />
<br>Your Comments<br>
<textarea rows="5" name="comments" cols="20" id="comments"></textarea><br>
<input type="submit" value="Add Comment"/>
</form><?php include"../templates/footer.php";?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Form Validation [*N0T WORKING*]

Post by social_experiment »

Could you perhaps explain how it isn't working :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
pHp_n0ob
Forum Commoner
Posts: 35
Joined: Mon Jul 09, 2012 7:30 am
Location: India

Re: Form Validation [*N0T WORKING*]

Post by pHp_n0ob »

social_experiment wrote:Could you perhaps explain how it isn't working :)
I made a form...and set validation..example:If a name would contain numbers(0-9),it would show an alert box written Name must contain Alphabets only.....similarlly,every condition has been set using js.....but its n0t workIng at all as I mentioned :(
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Form Validation [*N0T WORKING*]

Post by social_experiment »

Here's a tip that will help with readability of your code

Code: Select all

 // seperate statements with a new line 
 var i;
 if (i==0) {
    alert('Zero');
 }
 // instead of
 var i; if (i==0) { alert('Zero'); }
With regards to your current issue - use test() instead of match(); match() returns an array and you aren't testing for the presence of an array. The snippet below illustrates how to use it. From here you should be able to figure out what is next

Code: Select all

 var pattern = /\d/;
 
  if  (!pattern.test('ddd')) {
	alert('fail');
  }
Like PHP, javascript has character classes such as ' \d' which will match any digit between 0-9 so use those instead in your regular expressions.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply