JS表单验证不会发生

时间:2016-05-02 03:08:55

标签: javascript forms validation

当我测试这个表单时,函数validateJob正常工作,但似乎没有任何关于validateCheckbox或validateEmail的事情。我没有收到错误。

对于这个项目,我不需要表格实际做任何事情,但验证。我是初学者,所以我希望能找到一个小修复,而不是完全重构我到目前为止所做的。

这是我的剧本:

<script type="text/javascript">
/* <![CDATA[ */
function validateForm(form){
  var validation = true;
  validation &= validateJob(form);
  validation &= validateEmail(form);
  validation &= validateCheckbox(form);
  return validation;
}
function validateJob(){
    var jobSelected=false;

    for (var i=0; i<3; ++i) {
        if (document.forms[0].job[i].checked == true) {
        jobSelected=true;
        break;
        }
    }
    if (jobSelected == false) {
        window.alert("You must select a job option.");
        return false;
    }
    else {
       return true;
    }
//function to check check box
 function validateCheckbox() {
      var contactSelected = false;
    if (document.forms[0].contact.checked==true) {
       contactSelected=true;
    }
    if (contactSelected == false) {
    window.alert("You must approve being contacted.");
    return false;
    }
    else
    return true;
    }
//end checkbox check
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail() {
    var error="";
    var tfld = trim(document.forms[0].email);                       
 //     value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (document.forms[0].email.value == "") {
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {             
 //test email for illegal characters
        error = "Please enter a valid email address.\n";
    } else if (document.forms[0].email.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
    } 
    window.alert(error);
}

function confirmReset() {
        return resetForm;
}

/* ]]> */
</script>    

这是我的HTML:

    <form onSubmit="return validateForm(this)">
    <p> <label for="name">Full name: </label>
    <input type="text" name="fullname"></p>
    <p> <label for="email">Email: </label>
    <input type="text" name="email"></p>

    <p> <label for="job">I am applying to be a: </label><br/>
    <input type="radio" name="job">Waiter<br/>
    <input type="radio" name="job">Line cook<br/>
    <input type="radio" name="job">Other</p>
    <p> <label for="contact">Yes, you may contact me </label>
    <input type="checkbox" name="contact" ></p> 
   <input type="submit" value="Submit">
   <input type="reset" /></p>
</form>

1 个答案:

答案 0 :(得分:0)

您可以使用html requiredtitle属性,将<label>元素展示位置调整到<input>个元素之后; css :invalidcontent,相邻的兄弟选择器+

:not([type=radio]):invalid + [for]:after {
  content:attr(title);
}

[type=radio]:invalid ~ [for]:after, [type=checkbox]:invalid ~ [for]:after {
  content: attr(title);
}

:invalid + [for] {
  color:red;
  font-style:italic;
}
<form>
  <p>
    Full name:
      <input type="text" minlength="1" pattern="\w+" name="fullname" required />
    <label title="You must enter full name" for="name"></label>
  </p>
  <p>
    Email:
      <input type="text" name="email" required />
    <label title="Please enter a valid email address" for="email"></label>
  </p>

  <p>
    I am applying to be a:
    <br/>
    <input type="radio" name="job" required />Waiter
    <br/>
    <input type="radio" name="job" />Line cook
    <br/>
    <input type="radio" name="job" />Other
    <label title="You must select a job option." for="job"></label>
  <p>
    Yes, you may contact me
    <input type="checkbox" name="contact" required />
    <label title="You must approve being contacted." for="contact"></label>
  </p>
  <p>
    <input type="submit" value="Submit" />
    <input type="reset" />
  </p>
</form>