Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Javascript form validation (http://www.abandonia.com/vbullet/showthread.php?t=27092)

wackypanda 17-05-2011 10:51 AM

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?

Ohne Mitleid 18-05-2011 01:13 AM

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. :wacko:

Eagle of Fire 18-05-2011 03:38 PM

Anything that force me to turn JavaScript on a site I don't want to gives me bad vibes too...

wackypanda 19-05-2011 04:48 PM

Quote:

Originally Posted by Ohne Mitleid (Post 427406)
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.

Ohne Mitleid 20-05-2011 04:58 PM

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.:dunno:


The current time is 08:58 PM (GMT)

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