Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 17-05-2011, 11:51 AM   #1
wackypanda
Abandonia nerd

 
Join Date: Jan 2011
Location: ,
Posts: 51
Default Javascript form validation

The noob has returned.

Generally, if I want to do form validation I use the following format:

Code:
function validateForm(myForm)
{
if (failure conditions)
  {
  show error message;
  return false;
  }
return true;
}

<form onsubmit="return validateForm(this);"></form>
However, someone once told me it should be done like this:

Code:
function validateForm(myForm)
{
if (failure conditions)
  {
  show error message;
  }
myForm.submit();
}

<form onsubmit="validateForm(this); return false;"></form>
I don't quite get the logic behind the second one. It looks like it forces the user to turn on Javascript to submit the form. That gives me bad vibes.

What are your thoughts?
wackypanda is offline                         Send a private message to wackypanda
Reply With Quote
Old 18-05-2011, 02:13 AM   #2
Ohne Mitleid
Brazen Pangolin
 
Ohne Mitleid's Avatar


 
Join Date: Dec 2010
Location: ,
Posts: 202
Default

There are really too many variables when considering the creation and validation of a form. If you consider javascript to be a bad thing, is your form secured without it? Is the form client-side or server-side only, or a mix? Is the code going to include warnings that javascript is disabled (meaning required by the form)? Is the code in the first example as compact as it should be, or are you questioning the difference between the two based on structure and content vs. format for end result?

In order to answer the question to yourself, you would really need to define the parameters for the environment/users the form would be targeted for.

Hmm... someone is trying to make you think. This must be a school lesson.
Ohne Mitleid is offline                         Send a private message to Ohne Mitleid
Reply With Quote
Old 18-05-2011, 04:38 PM   #3
Eagle of Fire
Friendly Fire
 
Eagle of Fire's Avatar

 
Join Date: Sep 2004
Location: Valleyfield, Canada
Posts: 4,892
Default

Anything that force me to turn JavaScript on a site I don't want to gives me bad vibes too...
__________________
I'm on a hot streak... Literally.
Proud member of The Abandoned since 2005.
Eagle of Fire is offline                         Send a private message to Eagle of Fire
Reply With Quote
Old 19-05-2011, 05:48 PM   #4
wackypanda
Abandonia nerd

 
Join Date: Jan 2011
Location: ,
Posts: 51
Default

Quote:
Originally Posted by Ohne Mitleid View Post
There are really too many variables when considering the creation and validation of a form. If you consider javascript to be a bad thing, is your form secured without it? Is the form client-side or server-side only, or a mix? Is the code going to include warnings that javascript is disabled (meaning required by the form)?
I'm of the following schools of thought:

- If anything is being done server-side, server-side validation is inevitable (and therefore Javascript validation is not crucial).
- With Javascript, less is more.

I suppose the second could be used if the purpose of the form is such that Javascript has to be on anyway. It still doesn't seem like an ideal structure.

Last edited by wackypanda; 19-05-2011 at 05:58 PM.
wackypanda is offline                         Send a private message to wackypanda
Reply With Quote
Old 20-05-2011, 05:58 PM   #5
Ohne Mitleid
Brazen Pangolin
 
Ohne Mitleid's Avatar


 
Join Date: Dec 2010
Location: ,
Posts: 202
Default

Code:
function validateForm(myForm)
{
if (failure conditions)
  {
  show error message;
  return false;
  }
}

<form onsubmit="return validateForm(this);"></form>
This is the modified code from the first example. Follow the logic. There is no need for the return true. Onsubmit sends the input data to the validateForm function. The function initially checks for failure conditioins. If it fails, the error message is displayed, a false conditions response is returned and your code should be set up to continue looping until a return false response is not returned. If the function were to get past the failure conditions, it is assumed to be a success (return true).

At least that's how I understand it.
Ohne Mitleid is offline                         Send a private message to Ohne Mitleid
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript help? Maxor127 Programming 1 08-10-2009 07:30 AM
Something weird with JavaScript The Fifth Horseman Programming 2 15-07-2009 09:08 PM
Javascript And Firefox Abi79 Tech Corner 3 09-04-2006 06:53 AM

Thread Tools
Display Modes

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
 


The current time is 08:50 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.