PHP 表单数字验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16362058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP form numeric validation
提问by user2273149
I have been trying to use PHP to validate my form.
我一直在尝试使用 PHP 来验证我的表单。
The form asks users to enter details which will then get entered into a table in a database once the form has been validated.
该表单要求用户输入详细信息,一旦表单经过验证,这些详细信息将被输入到数据库中的表格中。
I have a customer ID field in the form, and I am trying to validate it to make sure that it has a value (compulsory field), contains only numeric characters, is exactly 6 digits in length and is a unique ID (i.e. does not already exist in the database).
我在表单中有一个客户 ID 字段,我正在尝试验证它以确保它有一个值(必填字段),只包含数字字符,长度正好是 6 位数字并且是一个唯一的 ID(即不已经存在于数据库中)。
Here is what I have so far :
这是我到目前为止所拥有的:
<body>
<?php
$temp = "";
$msg = "";
$rst = "";
if (isset($_POST["submit"])) {
$number = $_POST["custid"];
if(empty($number)) {
$msg = '<span class="error"> Please enter a value</span>';
} else if(!is_numeric($number)) {
$msg = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($number) != 6) {
$msg = '<span class="error"> The number entered was not 6 digits long</span>';
} else {
echo "valid";
}
}
?>
<h1>Customer Information Collection <br /></h1>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
<td><label for="custid">Customer ID (integer value): </label></td>
<td><input type="text" id="custid" name="custid" value="<?php echo $temp ?>" size=11 /><?php echo $msg; ?></td>
</tr>
<tr>
<td><label for="customerfname">Customer Frist Name: </label></td>
<td><input type="text" id="customerfname" name="fname" size=50/></td>
</tr>
<tr>
<td><label for="customerlname">Customer Last Name: </label></td>
<td><input type="text" id="customerlname" name="lname" size=50/></td>
</tr>
<tr>
<td><label for="customeraddress">Customer Address: </label></td>
<td><input type="text" id="customeraddress" name="custaddress" size=65/></td>
<td><label for="suburb"> Suburb: </label></td>
<td><input type="text" id="suburb" name="suburb"/></td>
</tr>
<tr>
<td>
State:<select name="state" id="state">
<option value="select">--</option>
<option value="ACT">ACT</option>
<option value="NSW">NSW</option>
<option value="NT">NT</option>
<option value="QLD">QLD</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="VIC">VIC</option>
<option value="WA">WA</option>
</select>
</td>
<td><label for="postcode"> Post Code: </label><input type="text" id="postcode" name="postcode" size=4/></td>
</tr>
</table>
<p><input type="submit" name="submit" value="Save Data"/> <input type="reset" value="Clear Form" />
</tr>
</form>
</body>
The problem I am having is that when I purposely enter incorrect values into the customer ID field, it doesn't give me any error. It just processes the incorrect values as if they were correct.
我遇到的问题是,当我故意在客户 ID 字段中输入不正确的值时,它不会给我任何错误。它只是处理不正确的值,就好像它们是正确的一样。
Any help would be really great! If any more information is needed, just ask.
任何帮助都会很棒!如果需要更多信息,请询问。
回答by Ryan
Here is your validation simplified, and with the correct operation to check the length of the id.
这是您的验证简化,并使用正确的操作来检查 id 的长度。
if(empty($number)) {
$msg = '<span class="error"> Please enter a value</span>';
} else if(!is_numeric($number)) {
$msg = '<span class="error"> Data entered was not numeric</span>';
} else if(strlen($number) != 6) {
$msg = '<span class="error"> The number entered was not 6 digits long</span>';
} else {
/* Success */
}
回答by chandresh_cool
In your case this is happening.
在你的情况下,这正在发生。
1) Checking empty string
1) 检查空字符串
2) Cheking numeric string and if it is numeric then checking the length.
2)检查数字字符串,如果是数字则检查长度。
Even after your validation fails for example if you enter invalid details, your validation captures the error and put it in $msg variable, but now your are not using that $msg variable if you echo that variable you can verify that it is working fine or not.
即使您的验证失败,例如,如果您输入了无效的详细信息,您的验证也会捕获错误并将其放入 $msg 变量中,但是现在您没有使用该 $msg 变量,如果您回显该变量,您可以验证它是否正常工作或不是。
Hence do
因此做
echo $msg;
to verify your validation.
验证您的验证。
回答by rizwan
if (isset($_POST['add-contact'])) {
$addPhone= $_POST['phone_no'];
$addCell = $_POST['cell_no'];
$addEmail= $_POST['email_address'];
$addPhone = trim($addPhone);
$addCell = trim($addCell);
$addEmail = trim($addEmail);
if (empty($addPhone) && empty($addCell) && empty($addEmail)) {
message("danger","At least one field is required...");
} elseif (strlen($addPhone) > 13) {
message("danger","Phone No length is too large greater then 13...");
} elseif (strlen($addCell) > 15) {
message("danger","Cell No length is too large greater then 15...");
} elseif (!is_numeric($addPhone)) {
message("danger","Invalid phone no ,only numbers are allowed..");
} elseif (!is_numeric($addCell)) {
message("danger","Invalid cell no ,only numbers are allowed..");
} else {
$q = "insert into contact_tbl(phone_no,cell_no,email)values('$addPhone','$addCell','$addEmail')";
mysql_query($q, $connect);
message("info", "Contact details added successfully...");
}
}