简体   繁体   中英

Check to see if text input field was skipped when form is submitted?

I have a form with text inputs and I am checking to see if there are any skipped text inputs when the form is submitted and have an alert that says that lines were skipped and they need to fill them in before they form can be submitted.

This is the code I am using and I get an error that nextEmptyLine is not defined if last line is blank.

I am getting the lin is the line count for that text input

var emptyLine = eval('document.forms.mainForm.itemLine_1_' + lin).value;
var nextEmptyLine = eval('document.forms.mainForm.itemLine_1_' + [lin+1]).value;    

if((emptyLine == "") && (nextEmptyLine != ""){
    alert("You seem to have skipped a line. Please go back and close up the lines. We do not allow for skipped lines." );
    submitOnce=false;
    return false;
}
>  var emptyLine = eval('document.forms.mainForm.itemLine_1_' + lin).value;

Wow, genuine dark ages code, where did you dig that up from? Sensible uses for eval are few and far between. As a general rule, never use it.

To check if every textarea element has a value other than the empty string or all whitespace, use a submit listener on the form:

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

Then the function is:

function checkTextareas(form) {
  var tas = form.getElementsByTagName('textarea');
  var re = /^\s*$/;

  for (var i=0, iLen=tas.length; i<iLen; i++) {
    if (re.test(tas[i].value)) {
      // textarea has only whitespace or nothing in its value
      // Tell the user why submit failed
      alert('Ooops! You missed a line');
      return false;
    } 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM