0

我在表格中有一张桌子。我希望用户输入所有三列详细信息。如果用户离开他们中的任何人,它应该会显示一条警告消息,例如“请输入姓名,不能为空”。如果所有字段都已填写,则只有插入应生效。

<table> <!--cellpadding=15 cellspacing=3-->
    <tr><td>Name</td><td>: <input type="text" name="name"></td></tr>
    <tr><td>Address</td><td>: <input type="text" name="address"></td></tr>
    <tr><td>Phone No</td><td>: <input type="text" name="dept_id"></td></tr>
</table>
4

7 回答 7

0

尝试使用标志变量。

<input type="text" name="name" id="nameId">
<input type="text" name="address" id="addressId">
<input type="text" name="dept_id" id="dept_id">

调用这个方法onSubmit()

function validate()
{
  var validate = false;
  if(document.getElementById("nameId").value == "")
  {  
      alert("NAME IS REQUIRED");
     validate   = true;
  }
  ...................    

  ...................   


return validate ;

}

于 2013-06-26T04:36:05.097 回答
0
$name = trim($_POST['name']);
if (empty($name))
    echo 'name is empty';

对其他字段执行此操作

于 2013-06-26T04:39:58.150 回答
0
<form method="get" enctype="text/plain"action="">
<table> <!--cellpadding=15 cellspacing=3-->

<? if($_GET['name'] == "") print "Please enter the name, it cannot be blank"; ?>
<tr><td>Name</td><td>: <input type="text" name="name"></td></tr>

<? if($_GET['address'] == "") print "Please enter the Address, it cannot be blank"; ?>  
<tr><td>Address</td><td>: <input type="text" name="address"></td></tr>

<? if($_GET['dept_id'] == "") print "Please enter the Phone No, it cannot be blank"; ?>    
<tr><td>Phone No</td><td>: <input type="text" name="dept_id"></td></tr>

</table>
</form>

并且不要忘记正确转义用户输入!!!

于 2013-06-26T04:40:56.127 回答
0

您可以javascript在表单提交上使用验证。看下面的例子。

myFormform nameonSubmit调用函数validateForm()

function validateForm()
{
   if (document.myForm.name.value == "")
   {
      alert("Please enter the name");
      document.myForm.name.focus();
      return false;
   }
   if (document.myForm.address.value == "")
   {
      alert("Please enter the address");
      document.myForm.address.focus();
      return false;
   }
   if (document.myForm.dept_id.value == "")
   {
      alert("Please enter the department id");
      document.myForm.dept_id.focus();
      return false;
   }
   return true;
}
于 2013-06-26T05:04:37.993 回答
0

你的完整代码

<script type="text/javascript">
function validateForm()
{
   if (document.test.name.value == "")
   {
      alert("Please enter the name");
      document.test.name.focus();
      return false;
   }
   if (document.test.address.value == "")
   {
      alert("Please enter the address");
      document.test.address.focus();
      return false;
   }
   if (document.test.dept_id.value == "")
   {
      alert("Please enter the department id");
      document.test.dept_id.focus();
      return false;
   }
   return true;
}
</script>



<form method="post" name="test" onsubmit="return validateForm();">
   <table> <!--cellpadding=15 cellspacing=3-->
       <tr><td>Name</td><td>: <input type="text" name="name"></td></tr>
       <tr><td>Address</td><td>: <input type="text" name="address"></td></tr>
       <tr><td>Phone No</td><td>: <input type="text" name="dept_id"></td></tr>
      <tr><td colspan="2"><input type="submit" name="submit_form" /></td></tr>
   </table>
</form>
于 2013-06-26T05:22:06.853 回答
0

$(function() {

$("#registration_form").validate({

    rules: {
        name: {
            required: true,
            lettersonly:true
        },
         contact: {
            required: true,
            numbers:true,
            minlength:10,
            maxlength:11
       }
}
 messages: {
        name: {
            required: "<h4><span style='color:red'>Please Enter your Name</span></h4>",
            lettersonly: "<h4><span style='color:red'>Numbers And Symbols Not Allowed</span></h4>"
        },
         contact: {
            required: "<h4><span style='color:red'>Please Enter your Phone number</span></h4>",
            numbers: "<h4><span style='color:red'>Character And Symbols Are Not Allowed</span></h4>",
            minlength: "<h4><span style='color:red'>Your phone Number Must Be At Least 10 Characters Long</span></h4>",
            maxlength: "<h4><span style='color:red'>Please Enter No More Than 11 Numbers</span></h4>"
        }
}
 submitHandler: function(form) {
        form.submit();
    }
});  }); 

  jQuery.validator.addMethod("numbers", function(value, element) {
  return this.optional(element) || /^[0-9]+$/i.test(value);
}, "numbers only please");

name 是 Name 字段的 id,contact 是 Phone number 字段的 id。

于 2015-12-18T10:28:01.537 回答
0

您可以使用 HTML 进行这种验证。

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

当用户未能填写此字段时,提交时会弹出消息说请填写此字段

于 2018-08-04T14:48:03.353 回答