Solved

check for blank form

Posted on 2009-04-06
12
315 Views
Last Modified: 2012-05-06
Hi - I have an online employment application form that I uses CGI script to send the data to my email. I do not want a blank form to get submitted. I need some simple javascript that can check to see if all fields are empty and if so not to submit the form.  A window can pop up saying something like the form has not been completed. What is the best way to achieve this? Thank you.
0
Comment
Question by:reesecup09
  • 5
  • 3
  • 3
12 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24083389
<form name="form1" onsubmit="return validate(this);">

<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit" value="submit!">
</form>

<script type="text/javascript">
function validate(formObj){
  if (formObj.text1.value == ''){
    alert('please input value of text1');
    formObj.text1.focus();
    return false;
  }else   if (formObj.text2.value == ''){
    alert('please input value of text2');
    formObj.text2.focus();
    return false;
  }
  return true;
}

</script>
0
 

Author Comment

by:reesecup09
ID: 24083518
If I use this script will I need to check each individual field to see if all of them are empty?
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 200 total points
ID: 24083569
like this?
<html>

<head><title></title></head>

<script type="text/javascript">

function validate2(formObj){

  var i=0;

  var formChilds = formObj.childNodes;

  var allBlank = true;

  for (i=0; i<formChilds.length;i++){

    if (formChilds[i].nodeName=='INPUT' && formChilds[i].type == 'text'){

      if (formChilds[i].value!=''){

        allBlank = false;

      }

    } 

  }

  

  if(allBlank){

    alert('no one field was inputted');

  }

  return (!allBlank);

}

 

</script>

</head> 

<body>

<form name="form1" onsubmit="return validate2(this);">

<input type="text" name="text1">

<input type="text" name="text2">

<input type="submit" value="submit!">

</form>

</body>

</html>

Open in new window

0
 

Author Comment

by:reesecup09
ID: 24093307
I am not good with javascript at all. I know just the basics so excuse my dumbness here, but do I just add the code to my html code as you have it or do I need to rename something. My form name is jobapp.
DO I ADD THIS BEWTEEN HEAD TAG?

<script type="text/javascript">

function validate2(formObj){

  var i=0;

  var formChilds = formObj.childNodes;

  var allBlank = true;

  for (i=0; i<formChilds.length;i++){

    if (formChilds[i].nodeName=='INPUT' && formChilds[i].type == 'text'){

      if (formChilds[i].value!=''){

        allBlank = false;

      }

    } 

  }

  

  if(allBlank){

    alert('no one field was inputted');

  }

  return (!allBlank);

}

 

</script>
 

ADD THIS TO FORM TAG?

onsubmit="return validate2(this);"

Open in new window

0
 

Author Comment

by:reesecup09
ID: 24093345
I just tried it and I get an alert when trying to submit the blank form. How many text boxes have to be filled in before the form can be submitted? Because I tried filling out two text bixes and hitting submit and I still got the alert.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:fsze88
ID: 24093744
example of #24083518
it only alert when all of field are empty..
do I need to rename something? No, the formObj passed into function validate2
How many text boxes have to be filled in before the form can be submitted? more than zero (1,2,3,4.....)
Because I tried filling out two text bixes and hitting submit and I still got the alert. ?  I tested on ff3, ie6.. that's okey....
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24094482
And yes, put the script in the head and modify your form tag as shown in the example. You need to scroll to see the form
0
 

Author Comment

by:reesecup09
ID: 24098435
Thanks. I get it now. But how do I fix ithe problem with alert? It should only pop up if the form is blank.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 300 total points
ID: 24098691
It should work. I do not think it makes things easier to understand to look at childNodes - many of them could be text nodes

I suggest you take the form below and change the fieldnames and action to what you have and leave the rest alone

If it STILL does not work, we need to see exactly what you did

<html>

<head><title></title></head>

<script type="text/javascript">

function validate2(formObj){

  var elems = formObj.elements;

  for (var i=0, n=elems.length; i<n;i++){

    if (elems[i].type == 'text' && elems[i].value!=''){

        return true

    } 

  }

  alert('All fields blank, please enter at least one field');

  return false;

}

 

</script>

</head> 

<body>

<form name="form1" onsubmit="return validate2(this);">

<input type="text" name="text1">

<input type="text" name="text2">

<input type="submit" value="submit!">

</form>

</body>

</html>

Open in new window

0
 

Author Comment

by:reesecup09
ID: 24098808
The script above is not what I used. I was using the one with childNodes. It's working now. Thank you you both so much for your help and patience. I appreciate it!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24098913
I know you did not, I just rewrote the script for you :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now