form validation

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1800 Views - Last Post: 09 December 2012 - 06:48 AM

#1 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

form validation

Posted 06 December 2012 - 04:54 AM

Hi

Below is the code where I am trying to Validate that the form is not empty and that the user typed his or her name in only in uppercase letters and send the form to a server program.

I got the uppercase validation right but but not the empty part,if the form is empty I get the enter only uppercase message.
<!DOCTYPE html>
<html>
<head>
<title>form validation</title>
<script type='text/javascript'>

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

	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(firstname, "Please enter only uppercase letters for your name")){

	}
	
	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 isAlphabet(elem, helperMsg){
	var alphaExp = /^[A-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

</script>
</head>
<body>

<form onsubmit='return formValidator()' >
 Name: <input type='text' id='firstname' />

<input type='submit' value='Submit' />
</form>
</body>
</html>

This post has been edited by Dormilich: 06 December 2012 - 05:35 AM
Reason for edit:: please use [CODE] [/CODE] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: form validation

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: form validation

Posted 06 December 2012 - 05:39 AM

View Posttechhead, on 06 December 2012 - 12:54 PM, said:

and that the user typed his or her name in only in uppercase letters

bad idea from the UX side. better accept any letters and upper-case them automatically.


View Posttechhead, on 06 December 2012 - 12:54 PM, said:

I got the uppercase validation right but but not the empty part,if the form is empty I get the enter only uppercase message.

the function you have seems ok so far, but why donít you use it? you just need to pass the textbox.
Was This Post Helpful? 2
  • +
  • -

#3 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 05:52 AM

how do I pass it???
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: form validation

Posted 06 December 2012 - 05:56 AM

thatís a trick question, right?

you know how to call functions and put in parameters?
Was This Post Helpful? 0
  • +
  • -

#5 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: form validation

Posted 06 December 2012 - 06:00 AM

On submit you call the function formValidator. In your formValidator you call the function isAlphabet, but you never call the function notEmpty!

You can use a similare if-statement to call your notEmpty(var1, var2) as you used for the isAlphabet(var1, var2).

EDIT
ooh Dormilich was just faster then me :P

This post has been edited by Anthonidas: 06 December 2012 - 06:02 AM

Was This Post Helpful? 1
  • +
  • -

#6 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:02 AM

I have- form onsubmit='return formValidator()'
Was This Post Helpful? 0
  • +
  • -

#7 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:12 AM

You mean like that, I tried and it didn't work and the other validater didn't work

function formValidator(){  

// Make quick references to our fields  

var firstname = document.getElementById('firstname');  

// Check input that it appears in the form! 

if(isAlphabet(firstname, "Please enter only uppercase letters for your name")){  
   
	}  

return false;  

 }  
if(notEmpty(firstname, "Please enter your name")){
}
return false;
}

This post has been edited by Dormilich: 06 December 2012 - 06:14 AM
Reason for edit:: please use [CODE] [/CODE] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#8 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: form validation

Posted 06 December 2012 - 06:12 AM

yes, but your formValidator() does not contain any call to notEmpty()
Was This Post Helpful? 1
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: form validation

Posted 06 December 2012 - 06:15 AM

it does contain, but the function quits before it can use it because of the return statement on line #13, which is always executed.
Was This Post Helpful? 1
  • +
  • -

#10 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:15 AM

how do I call it
Was This Post Helpful? 0
  • +
  • -

#11 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:20 AM

ok thanks done that I removed return but i still keep getting the message "Please enter only uppercase letters for your name" when it is empty
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: form validation

Posted 06 December 2012 - 06:25 AM

of course, no letters are no upper-case letters, which also trigger the isAlphabet() error.
Was This Post Helpful? 0
  • +
  • -

#13 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:29 AM

Yes I realize that so how do I fix it so when the field is empty I can get the message please enter you name
Was This Post Helpful? 0
  • +
  • -

#14 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Re: form validation

Posted 06 December 2012 - 06:34 AM

I've fixed I just had it in the wrong order, Ishould of had notEmpty function before isAlphabet, but when its empty I still get the "Please enter only uppercase letters for your name" message straight after the "Please enter your name message"
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: form validation

Posted 06 December 2012 - 07:20 AM

either return on the first error or make an else if() clause.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2