Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 12:19 AM   PM User | #1
kiefersmokerlan
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kiefersmokerlan is an unknown quantity at this point
Unhappy Validation script

Hello everyone,

So I am practicing on my HTML5/JavaScript skills, but I am a bit stumped and would like some input. I am using a JavaScript that I wrote and used in the past, that makes sure that all the text boxes are filled before proceeding to the next page. The validating works fine, but there is one error, which I have never encountered before. I notice that when the text boxes are empty, an alert box appears notifying the user to fill out all the required fields, but it continues to the next page, instead of allowing the user to correct the problem, then continue accordingly. Here is my code:
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function validate(){
	var valid = true;
	if (document.regpage.usrNm.value==""||document.regpage.passWrd.value==""||document.regpage.cnfrmPsswrd.value==""||document.regpage.state.value==""||document.regpage.zip.value=="")
	    valid = false;
	if (valid == false)
		alert("You must fill out ALL required fields!");
	else
		document.regpage.submit();}
</script>

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

<body>

<div class="logIn">
<form name="regpage" action="reg.php" method="post">
<p>Easy sign-up! Please fill out all the required fields to get started!
</p>
User Name<font color="red">*</font>:
<input type="text" name="usrNm" value="" /><br />
Password<font color="red">*</font>:
<input type="text" name="passWrd" value="" /><br />
Confirm Password<font color="red">*</font>:
<input type="text" name="cnfrmPsswrd" value="" /><br />
State<font color="red">*</font>:
<input type="text" name="state" value="" /><br />
Zip Code<font color="red">*</font>:
<input type="text" name="zip" value="" /><br />
<input type="submit" value="Register" onclick="validate()" />
</form>
</div>

</body>
</html>
Any help would be appreciated. As many of you experienced programmers may notice; I am somewhat new to web development and programming.
kiefersmokerlan is offline   Reply With Quote
Old 01-25-2013, 01:20 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,166
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The quick and dirty way:

Change <input type="submit"> to <input type="button">.

So long as it is a submit button, then it will submit the <form>!

***********

Better way. Get rid of the onclick on the submit button completely.

Change the <form> to:

<form name="regpage" action="reg.php" method="post" onsubmit="return validate(this);">

Change your JS to:
Code:
function validate(form)
{
    if (    form.usrNm.value==""
         || form.passWrd.value==""
         || form.cnfrmPsswrd.value==""
         || forme.state.value==""
         || form.zip.value ==""
    ) {
        alert("You must fill out ALL required fields!");
        return false;
    }
    return true;
}
But having said all the above...

WHy bother? That validation is essentially useless.

If the user enters a SINGLE SPACE (or any other character...or any other completely illogical value) for each field, it will pass validation.

If you are going to validate, then *REALLY* validate. Don't both with virtually useless code.

There are literally HUNDREDS (if not thousands) of posts in this very forum relating to form validation. Go look around and find some *good* validation.

p.s.: And I hope you are *ALSO* validating the <form> on your PHP page! Obviously form validation in JavaScript is useless if somebody turns off JavaScript. Or if a hacker hits your site without even using your code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-25-2013 at 01:24 AM..
Old Pedant is online now   Reply With Quote
Old 01-25-2013, 02:42 AM   PM User | #3
kiefersmokerlan
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
kiefersmokerlan is an unknown quantity at this point
Question

Thank you for you help. Being new to JavaScript I can use all the advice and knowledge that I can use. I will definitely go through different posts on this forum to learn more.

I was going to use the reg.php to connect to a database I created, and transfer the information from the text boxes in the reg.html file, to the database. I suppose that you're suggesting to write a validation script on the reg.php page as well to make more secure to attacks and an actual effective validation.
kiefersmokerlan is offline   Reply With Quote
Old 01-25-2013, 03:09 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,166
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by kiefersmokerlan View Post
I was going to use the reg.php to connect to a database I created, and transfer the information from the text boxes in the reg.html file, to the database. I suppose that you're suggesting to write a validation script on the reg.php page as well to make more secure to attacks and an actual effective validation.
** EXACTLY! **

It's really very important. Trust me, SOMEDAY you will be hit by hackers who will simply look at your <form> code and start submitting usernames and passwords as fast as they can "spoofing" your site. They won't even use your actual <form>, much less your JS code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-25-2013, 04:54 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Also get rid of the alert and update the error message in the page itself instead - most browsers now either allow alerts to be turned off or even provide an option in the alert to turn off JavaScript.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:08 PM.


Advertisement
Log in to turn off these ads.