dpii73380 2015-11-25 13:48
浏览 253
已采纳

JavaScript验证 - document.getElementById在Bootstrap模式中不起作用

I am using an onSubmit() function to validate form entries.

The form and validation function work OK,if the form is in a standalone php file or an html file.

But when the form is embedded in a Bootstrap modal the JS validation function throws an error message. [object HTMLInputElement]

I have tried changing document.getElementById('uemail') to document.getElementById('uemail').value as recommended in another answer, but no value has apparently been passed to the function.

If I remove the onSubmit() attribute then the values are passed correctly to the action script on the server.

This must be common usage. What am I doing wrong?

This is an abbreviated section of my code:

<script>
function formCheck() {
    var valid = true;
    //alert('in function');
    var email = document.getElementById('uemail');
    alert('email:' + email);
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        alert('Please provide a valid email address');
        email.focus;
        valid = false;
    }
    return valid;
}
</script>


<form id="regform" action="action.php" method="post" onSubmit="return formCheck()">
    Email: <input type="text" name="uemail" id="uemail" size="50" value="" required><span id ="ast">*</span><br>
    <input type="submit" name="Submit" value="Register" />
</form>
  • 写回答

1条回答 默认 最新

  • dongqianwei6664 2015-11-25 13:50
    关注

    EDIT: looks like you can't do document.getElementById, you can do this instead:

    function formCheck(formEl) {
        //...
        var email = formEl.getElementsByTagName("input")[0].value;
        // this will get the first input-element in your form
    

    and

    onsubmit="return formCheck(this)"
    

    Mistake: The variable email you are testing is a HTMLElement, not a string:

    enter image description here

    Solution: set email to the value of the HTMLInputElement:

    var email = document.getElementById('uemail').value;
    

    also email.focus; is not doing anything, use email.focus();

    Demo:

    function formCheck() {
      var valid = true;
      //alert('in function');
      var email = document.getElementById('uemail').value;
      alert('email:' + email);
      var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (!filter.test(email)) {
        alert('Please provide a valid email address');
        email.focus;
        valid = false;
      }
      return valid;
    }
    <form id="regform" action="action.php" method="post" onsubmit="alert(formCheck())">
      Email:
      <input type="text" name="uemail" id="uemail" size="50" value="" required><span id="ast">*</span>
      <br>
      <input type="submit" name="Submit" value="Register" />
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?