Textarea Form Validation

O

Openside

Hi,

I am fairly green when it comes to JavaScript. I have the Form
Validation below for 3 textarea fields. It works fine, but if none of
the fields are filled out then 3 consequtive alert boxes pop up.How can
I modify the script to only prompt the user one field at a time?

The Script I have is:

<script language="JavaScript">
<!--

function valform ( )
{
valid = true;

if ( document.recipeform.directions.value == "" )
{
alert ( "Please enter the directions of the recipe." );
valid = false;
}

if ( document.recipeform.description.value == "" )
{
alert ( "Please enter the description of the recipe." );
valid = false;
}

if ( document.recipeform.ingredients.value == "" )
{
alert ( "Please enter the ingredients of the recipe." );
valid = false;
}

return valid;
}

//-->
</script>

thanks,

Openside
 
T

TheBigSearchEngine

You can use something like this:

<script language="JavaScript">
<!--

function valform () {
var msg = '';

if ( document.recipeform.directions.value == "" ) {
msg .= "Please enter the directions of the recipe.\n";
}


if ( document.recipeform.description.value == "" ) {
msg += "Please enter the description of the recipe.\n";
}


if ( document.recipeform.ingredients.value == "" ) {
msg .= "Please enter the ingredients of the recipe.\n";
}

if(msg != '') {
alert(msg);
return false;
}
return true;
}

//-->
</script>
 
T

Thomas 'PointedEars' Lahn

TheBigSearchEngine wrote:
^^^^^^^^^^^^^^^^^^
Posting with such a ridiculous "name" does not increase credibility of a
poster. Posting such ridiculous nonsense as below is only another proof
to justify this preconception.
You can use something like this:

But you should not. Not at all.
<script language="JavaScript">

The `language' attribute is deprecated, the `type' attribute is required in
HTML 4:


Trying to comment out `script' element content has always been nonsense.
function valform () {
var msg = '';

if ( document.recipeform.directions.value == "" ) {

Standards compliant referencing would be

if (document.forms['recipeform'].elements['directions'].value == "")
{

However, it is more efficient and less error-prone if one passes the `this'
reference in the `onsubmit' event handler attribute value to the validation
method, and use its named argument instead. Say if the method was called
with

<form ... onsubmit="return valform(this);">

and defined as

function valform(f)
{
//
}

one would use

if (f.elements['directions'].value == "")

instead. Because the reference `f.elements' would be used afterwards, it
is more efficient to assign it to a local variable and use that variable
instead. This approach also facilitates a basic feature test:

function valform(f)
{
var es;
if (f && (es = f.elements))
{
if (es['directions'].value == "")
{
// ...
}

// ...
}

return true;
}

msg .= "Please enter the directions of the recipe.\n";

The string concatenation operator in ECMAScript implementations is `+',
not `.' (as in PHP). The string concatenation operation `+=' is generally
inefficient compared to the available alternatives (even in PHP).

However, ...
}


if ( document.recipeform.description.value == "" ) {
msg += "Please enter the description of the recipe.\n";
}


if ( document.recipeform.ingredients.value == "" ) {
msg .= "Please enter the ingredients of the recipe.\n";
^^
(See above.)

.... this approach is hardly user-friendly. A user-friendly approach would
present the user with a list of data that are missing, embedded in the
message, not the same sentence with only different parts again and again.
if(msg != '') {
alert(msg);
return false;
}
return true;
}

//-->
</script>

Therefore:

<meta http-equiv="Content-Script-Type" content="text/javascript">
....
<form ... onsubmit="return valform(this);">
<script type="text/javascript">
function valform(f)
{
var es;
if (f && (es = f.elements))
{
    var
// or `new Array()', for JavaScript 1.1 and 1.2
aMissing = [],

// or `new RegExp()', for JavaScript 1.1/JScript 2.0
rxNotEmpty = /\S/;

    if (!rxNotEmpty.test(es['directions'].value))
{
// or `aMissing[aMissing.length] = "..."',
// for JavaScript 1.2/JScript 2.0
    aMissing.push("- directions of the recipe");
    }

    if (!rxNotEmpty.test(es['description'].value))
{
        aMissing.push("- description of the recipe");
    }

    if (!rxNotEmpty.test(es['ingredients'].value))
{
        aMissing.push("- ingredients of the recipe.");
}

    if (aMissing.length > 0)
{
       window.alert(
'Please provide the following information:\n\n'
+ aMissing.join("\n"));
      return false;
    }
}

    return true;
}
</script>
...
</form>

However, neither of the above is news here. Ask yourself
("TheBigSearchEngine") for "form validation".


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 3/4/2006 4:01 PM:
TheBigSearchEngine wrote:
^^^^^^^^^^^^^^^^^^
Posting with such a ridiculous "name" does not increase credibility of a
poster.

It's no different than you using "PointedEars" in your name. Usenet is
anonymous, deal with it.
Posting such ridiculous nonsense as below is only another proof
to justify this preconception.
Agreed

function valform () {
var msg = '';

if ( document.recipeform.directions.value == "" ) {

Standards compliant referencing would be

if (document.forms['recipeform'].elements['directions'].value == "")
{

But that is nonsense and has been debated here so many times it gets
old. Either way is "acceptable" and "compliant".
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 3/4/2006 4:01 PM:

It's no different than you using "PointedEars" in your name.

I expected as much from you. One day you will probably recognize the
difference between a nickname provided along with the real name, and a
pseudonym.
Usenet is anonymous, deal with it.

Usenet is _not_ anonymous. One may use a pseudonym, but that is a
completely different thing.


PointedEars
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 3/4/2006 5:48 PM:
I expected as much from you. One day you will probably recognize the
difference between a nickname provided along with the real name, and a
pseudonym.

I am well aware of the difference Thomas. But to be that pedantic about
the name someone uses in Usenet is a waste of time, no matter the
intentions. It's irrelevant.
Usenet is _not_ anonymous. One may use a pseudonym, but that is a
completely different thing.

You have no way of emailing me.
You have no way of contacting me other than through Usenet.
You have no idea if my name is really Randy Webb or not.
All that you know about me is what I tell you about me.

Sure, you can make a few educated guesses based on headers but that is it.

Call it what you want, that makes it close enough to anonymous for most
people.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top