6 Replies - 1307 Views - Last Post: 02 February 2009 - 10:09 PM

#1 naash  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 115
  • Joined: 26-August 07

Form validation

Posted 29 January 2009 - 09:48 AM

The attached page has 3 entries to be made.

Now I want to validate them, that is, if the form is submitted with even one blank field an error must be indicated.

I have put the validation code but it doesn't work upon empty form submission.

Help !

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: Form validation

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 5935
  • View blog
  • Posts: 10,346
  • Joined: 28-September 07

Re: Form validation

Posted 29 January 2009 - 10:16 AM

Moved to Javascript :)
Was This Post Helpful? 0
  • +
  • -

#3 Mouse86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-June 08

Re: Form validation

Posted 29 January 2009 - 10:40 PM

I'm having the same problem


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css" />

<!--<style type='text/javascript'>
function testFunc(field, alerttxt)
	{
	with(field)
	{
	apos=value.indexOf("@");
	dotpos=value.indexOf(".");
	if(apos<1 || dotpos-apos<2)
	{
	alert(alerttxt);
	return false;
	}
	else{return true;}
	}
	}
function validateForm(thisform)
{
with(thisform)
{
	if(testFunc(emails, "BAD EMAIL")==false)
	{email.focus(); return false}
}
}
</style>-->
</head>
<body>
<form action="../notepad++/mainpage.html" onsubmit="validateForm(thisform)" method="post">
<table align='left'>
<tr><td align='right'>
Username: <input type="text" name="users" />
</td></tr>
<tr><td align='right'>
Password: <input type="password" name="passes" />
</td></tr>
<tr><td align='right'>
Confirm Password: <input type="password" name="confirmpasses" />
</td></tr>
<tr><td align='right'>
Email: <input type="text" name="emails" />
</td></tr>
<tr><td align='right'>
Confirm Email: <input type="text" name="confirmemails" />
</td></tr>
<tr><td align='center'>
<input type="submit" value="SUBMIT" class="button" />
</td></tr>
</table>
</form>
</body>
</html>



Why the hell won't it create a popup "BAD EMAIL" when the email is blank?
I've used evey method of onsubmit I could think of
onsubmit="validateForm(thisform)"
onsubmit="java script: validateForm(thisform)"
onsubmit="java script: return validateForm(thisform)"
onsubmit="return validateForm(thisform)"


Where am I wrong??
Was This Post Helpful? 0
  • +
  • -

#4 thebath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-May 08

Re: Form validation

Posted 30 January 2009 - 02:13 AM

Got the same problem aswell, no point starting a new thread might as well join in,
Here's my java code
<script type='text/javascript'>

function formValidator(){
	// Make quick references to our fields
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var addr = document.getElementById('addr');
	var phone = document.getElementById('phone');

	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(name, "Please enter only letters for your name")){
	if(emailValidator(email, "Please enter a valid email address")){
		if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
			if(isNumeric(phone, "Please enter a valid Phone Number")){
				
							return true;
						}
					}
				}
			}
		
	
	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		window.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{
		window.alert(helperMsg);
		elem.focus();
		return false;
	}
}

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

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


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{
		window.alert(helperMsg);
		elem.focus();
		return false;
	}
}</script> 


And my html form

<form onsubmit='return formValidator' > Name:<br />

  <input name="to" value="contact@somewhere.com" type="hidden" /> <input onfocus="this.value=''" name="name" id="name" value="Please Enter Your Name" size="40" type="text" />
  <br />

Mail:<br />

  <input onfocus="this.value=''" name="email" id="email" value="Enter your e-mail address" size="20" /> <br />
Address:<br />

  <input onfocus="this.value=''" value="Address" id="addr" name="addr" size="20" /><br />

Phone:<br />

  <input onfocus="this.value=''" value="Phone Number" id="phone" name="phone" size="20" />
  <br />

Comment:<br />

  <textarea onfocus="this.value=''" wrap="hard" cols="40" rows="5" name="message">Enter your enquiry here</textarea> <br />

  <input value="Send" type="submit" /> 
  <input value="Reset" type="reset" /></form>
 


Nothing is being validated and the javascript is from http://www.tizag.com...ascriptform.php

This post has been edited by thebath: 30 January 2009 - 02:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 thebath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 11-May 08

Re: Form validation

Posted 30 January 2009 - 04:48 AM

Well i found mine, was missing parenthesis after a function call

This post has been edited by thebath: 30 January 2009 - 04:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Form validation

Posted 30 January 2009 - 09:56 AM

Mouse:
You have the javascript commented out with <!-- -->
Javascript goes in script tags, not style tags
If you want the form to not submit when your validation returns false, you'd want
onsubmit="return validateForm(this);"



I haven't looked at your script for syntax errors, that's something you should be doing. If you are using Firefox go to Tools->Error Console, clear it, and then reload your page to see what errors it finds.
Was This Post Helpful? 0
  • +
  • -

#7 Mouse86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-June 08

Re: Form validation

Posted 02 February 2009 - 10:09 PM

I made the corrections and get a 'ValidateForm not defined on line 1' error????

This post has been edited by Mouse86: 02 February 2009 - 10:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1