通过JavaScript强制HTML表单验证

时间:2015-12-21 19:24:46

标签: javascript html

我创建了一个HTML表单,其中有两个按钮(而不是提交按钮),每个按钮都以编程方式将表单发送到唯一的表单操作地址。

<form id="formExample">
<input type="text" id="input1" required>
<label type="button" onClick="form1()">Form Action 1</label>
<label type="button" onClick="form2()">Form Action 2</label>
</form>

脚本:

form = document.getElementById("formExample");

function form1() {
    form.action="example1.php";
    form.submit();
}
function form2() {
    form.action="example2.php";
    form.submit();
}

运作良好,回应您按下的按钮。但是,之前使用相同的html表单验证(使用“提交”按钮时),不再显示提示,无论是否有输入,表单都会发送。

我已经读过,因为我以编程方式调用form.submit(),它绕过了表单的onSubmit()函数,这是验证发生的地方。

我的问题是:我可以以编程方式强制onSubmit()以便弹出验证吗?我必须明确表示我不想创建JavaScript表单验证,即使用警报;相反,当您单击提交时,使用JavaScript强制执行此处的HTML验证:https://jsfiddle.net/qdzxfm9u/

4 个答案:

答案 0 :(得分:3)

也许是这样的:

var form = document.getElementById("formExample");

function form1() {
  form.action="example1.php";
}

function form2() {
  form.action="example2.php";
}
<form id="formExample">
  <input type="text" id="input1" required>
  <input type="submit" onClick="form1()" value="Form Action 1" />
  <input type="submit" onClick="form2()" value="Form Action 2" />
</form>

答案 1 :(得分:3)

您只需将按钮的类型更改为submit,然后从JS部件中删除form.submit()

因此HTML部分变为:

<form id="formExample">
<input type="text" id="input1" required>
<button type="submit" onClick="form1()">Form Action 1</button>
<button type="submit" onClick="form2()">Form Action 2</button>
</form>

这样,单击任何按钮会自行提交,但在执行JS部分之前:

form = document.getElementById("formExample");

function form1() {
    form.action="example1.php";
}
function form2() {
    form.action="example2.php";
}

修改

警告:我最初的解决方案是基于OP HTML部分的副本,其中“伪按钮”使用了一个奇怪的元素<label type="input"...>,因此我读得(太快)就好像它是{{1只需将<button type="button"...>type更改为input! 这样,它无法按预期工作。

现在在上面的代码中进行了更正。

答案 2 :(得分:2)

如何制作一个dropdown列表 - 可以是单选按钮 - 包含表格两个动作,一个提交按钮,如JS Fiddle,然后有一个功能表格submit

&#13;
&#13;
var form = document.getElementById("formExample"),
    select = document.getElementById("slct");

form.addEventListener('submit', function() {
  if (select.value == 1) {
    form.action = "example1.php";
  } else {
    form.action = "example2.php";
  }
  // alert for demo only
  alert(form.action);
  form.submit();
});
&#13;
<form id="formExample">
  <input type="text" id="input1" required>
  <select id="slct" required>
    <option></option>
    <option value="1">Form Action 1</option>
    <option value="2">Form Action 2</option>
  </select>
  <button type="submit">Submit</button>
</form>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

function togglePassword(el){
     var checked = el.checked;
     if (checked) {
        document.getElementById("password").type = 'text';
        document.getElementById("toggleText").textContent= "Hide";
     } else {
        document.getElementById("password").type = 'password';
        document.getElementById("toggleText").textContent= "Show";
     }
  }

  function login()
  {
     var uname = document.getElementById("uname").value;
     var password = document.getElementById("password").value;
     if(uname === '')
     {
        alert("Please enter the user name.");
     }
     else if(password === '')
     {
        alert("Enter the password");
     }
     else
     {
        alert('Login Successful. Thank You!');
     }
  }

  function clearFunc()
  {
     document.getElementById("uname").value="";
     document.getElementById("password").value="";

<script type="text/javascript">
      function togglePassword(el){
         var checked = el.checked;
         if (checked) {
            document.getElementById("password").type = 'text';
            document.getElementById("toggleText").textContent= "Hide";
         } else {
            document.getElementById("password").type = 'password';
            document.getElementById("toggleText").textContent= "Show";
         }
      }

      function login()
      {
         var uname = document.getElementById("uname").value;
         var password = document.getElementById("password").value;
         if(uname === '')
         {
            alert("Please enter the user name.");
         }
         else if(password === '')
         {
            alert("Enter the password");
         }
         else
         {
            alert('Login Successful. Thank You!');
         }
      }

      function clearFunc()
      {
         document.getElementById("uname").value="";
         document.getElementById("password").value="";
      }  
   </script>     
/* heading  */
h1 {
    display: block;
    font-size: 2em;
    font-weight: bold;
    /* padding: 0% 1% 3% 6.5%; */
    margin: 0% 35% -10% 36%;
}


h1:hover{
  color:#4499d9 ;
  transform: translateY(-5px);
}


/* bg image */
body {
  background-image: url('img/bg4.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 100% 100%;
}


/* Bordered form */
form {
 /*  border: 13px solid black; */
  width: 27%;
  height: auto;
  position: absolute;
  left: 10%;
  margin-left: -3px;
  top: 18%;
}

/* Full-width inputs */
input[type=text], input[type=password] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

/* Set a style for all buttons */
button {
  background-color: #17234b;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 49%;
}

/* Add a hover effect for buttons */
button:hover {
  background-color: #4499d9;
   transform: translateY(-5px);
  box-shadow: 1px 3px 7px #6f6d72;
}

#toggleText {
    display: block;
}


/* Center the avatar image inside this container */
.imgcontainer {
  text-align: center;
  margin: 24px 0 12px 0;
}

/* Avatar image */
img.avatar {
     width: 30%;
    border-radius: 20%;
     box-shadow: 1px 3px 9px #6f6d72;
}

img.avatar:hover{
   transform: translateY(-5px);
  box-shadow: 7px 9px 9px #6f6d72;
}

/* Add padding to containers */
.container {
  padding: 16px;
}

span.buttons{
    width: 100%;
    display: flow-root;
}

#toggleText{
  float: left;
}

#toggle{
  float: left;
}
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
   <h1>LOGIN PANEL</h1>
   <!-- Login Form -->
   <div class="form"> 
      <form >
         <div class="imgcontainer">
            <img src="img/Login.jfif" alt="Login Avatar" class="avatar">
         </div>
         <div class="container">
            <label for="uname"><b>Username</b></label>
            <input type="text" placeholder="Enter Username" id="uname" name="uname"/>
            <label for="password"><b>Password</b></label>
            <input type='password'placeholder="Enter Your Password" name="password" id='password'/>
            <input type='checkbox' id='toggle' onchange='togglePassword(this)'><span id='toggleText'>Show</span>
            <span class="buttons">
               <button type="submit" value="Reset" onclick="clearFunc()" class="btn">Reset</button>
               <button type="submit" value="Login" class="btn" onClick="login()">Login</button>
            </span>
         </div>
      </form>
   </div> 


 
</body>
</html>