验证文本框

时间:2013-06-26 04:30:50

标签: php html mysql textbox

我在表格中有一张桌子。我希望用户输入所有三列详细信息。如果用户离开其中任何一个,它应该显示一条警告消息,如“请输入名称,它不能为空”。如果填写了所有字段,那么只有插入才能生效。

<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>

7 个答案:

答案 0 :(得分: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 ;

}

答案 1 :(得分:0)

$name = trim($_POST['name']);
if (empty($name))
    echo 'name is empty';

为其余字段执行此操作

答案 2 :(得分: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>

并且不要忘记正确地逃避用户输入!!!

答案 3 :(得分: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;
}

答案 4 :(得分: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>

答案 5 :(得分: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");

名称是姓名字段的ID,联系人是电话号码字段的ID。

答案 6 :(得分:0)

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

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

如果用户未能填写此字段,则在提交时会弹出一条消息,提示您请填写此字段