表单验证时不验证某条字段_PHP表单验证:检查字段

表单验证时不验证某条字段

Now that we know how to use pattern matching with regular expressions, we can use to check the information that the user as entered into a form. For this example, I will keep the form fields very simple: we want to check that the user’s first name, postal code, email address, and province have been filled out correctly.

现在,我们知道如何将模式匹配与正则表达式结合使用,我们可以使用来检查用户输入到表单中的信息。 对于此示例,我将使表单字段非常简单:我们要检查是否正确填写了用户的名字,邮政编码,电子邮件地址和省份。

For the purposes of clarity, I will also do the form validation on a separate page from the form itself. The form will be on a page called form.html, and the receiving page, the action of the form, will be formhandler.php This would typically not be the case: it's much more common to do all of these operations on a single page, but that can be a little confusing at first, and is an approach I will cover in a later entry.

为了清楚起见,我还将在与表单本身不同的页面上进行表单验证。 表单将位于名为form.html的页面上,而接收页面(即表单的action将为formhandler.php 。通常不是这种情况:在单个页面上执行所有这些操作更为常见,但起初可能会有些混乱,这是我将在以后的文章中介绍的一种方法。

First, the code for the form, which is a simple document:

首先,表单的代码,这是一个简单的文档:

<form method="post" action="formhandler.php">
	<fieldset>
		<legend>Please enter your information</legend>
			<label for="firstname" accesskey="f">First name</label>
			<input type="text" name="firstname" id=firstname" size="30">
			<label for="postalcode" accesskey="p">Postal code</label>
			<input type="text" name="postalcode" id=postalcode" size="9">
			<label for="email" accesskey="e">eMail address</label>
			<input type="email" name="email" id=email" size="50">
			<label for="province" accesskey="t">Province / Territory</label>
			<select name="province" id=province">
				<option value="" selected>-- select one --
				<option value="AB">Alberta
				<option value="BC">British Columbia
				<option value="MB">Manitoba
				<option value="NB">New Brunswick
				<option value="NL">Newfoundland and Labrador
				<option value="NS">Nova Scotia
				<option value="NT">Northwest Territories
				<option value="NU">Nunavut
				<option value="ON">Ontario
				<option value="PE">Prince Edward Island
				<option value="QC">Québec
				<option value="SK">Saskatchewan
				<option value="YT">Yukon
			</select>
		<input type="submit" value="Go">
</fieldset>
</form>

Next, the page that is the action for this form, formhandler.php. The first thing to do is simplify the variables received, just to make life slightly easier, as well as creating some variables that we will use later:

接下来,是此表单的操作页面formhandler.php 。 首先要做的是简化接收到的变量,只是为了使生活更轻松,以及创建一些稍后将使用的变量:

$firstname = $_POST['firstname'];
$postalcode = $_POST['postalcode'];
$email = $_POST['email'];
$province = $_POST['province'];
$errorflag = false;
$errorfirstname = false;
$errorpostalcode = false;
$erroremail = false;
$errorprovince = false;

I’m then going to set up the patterns for the fields:

然后,我将为字段设置模式

$namepattern = “/^[[:alpha:].’ -]{2,15}$/”;
$postalpattern = "/[A-Z][0-9][A-Z][0-9][A-Z][0-9]/";
$emailpattern =
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,63}(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/";

As the user may have entered a hyphen or a space when entering her postal code, I’m going to create an array of those values. I’ll then remove any occurrences of hyphens or spaces with nothing at all (effectively removing them) and place the result, converted to uppercase, in a variable:

由于用户在输入邮政编码时可能输入了连字符或空格,因此我将创建一个包含这些值的数组 。 然后,我将删除连字符或空格的任何出现(完全删除它们)(有效删除它们),并将转换为大写形式的结果放入变量中:

$rem_array = array(" ","-");
$temp_postalcode = strtoupper(str_replace($rem_array,"", $postalcode));

Finally, we check that the entered information matches the expected patterns or values. If they do not, we want to do two things:

最后,我们检查输入的信息是否与预期的模式或值匹配。 如果他们不这样做,我们想做两件事:

  1. Record a specific error for that form field;

    记录该表单字段的特定错误;
  2. Record that there is something wrong with the form as a whole.

    记录整个表单有问题。

Naturally, there are more efficient ways of doing this, but I find this method to be the easiest to understand to start. The tests would be something like the following:

当然,有更有效的方法可以做到这一点,但是我发现这种方法最容易理解。 测试将类似于以下内容:

if (!preg_match($namepattern, $firstname)) { 
	$errorflag = true; $errorfirstname = true;
}
if (!preg_match($emailpattern, $email)) { 
	$errorflag = true; $erroremail = true;
}

Note that we test our cleaned-up $temp_postalcode variable, not the original postal code:

请注意,我们测试了清理后的$temp_postalcode变量,而不是原始的邮政编码:

if (!preg_match($postalpattern, $temp_postalcode)) { 
	$errorflag = true; $errorpostalcode = true;
}

There’s no need to pattern-test $province, as that will always be a known set of values; if the user has not chosen a province or territory, $province will be blank:

无需对$province进行模式测试,因为这将始终是一组已知值。 如果用户未选择省或地区,则$province将为空白:

if (!$province) { 
	$errorflag = true; $errorprovince = true;
}

Now we have $errorflag recording if there is something wrong with our form. We can now decide whether to process the information, or to alert the user that she has made an error:

现在,如果表单存在问题,我们将记录$errorflag 。 现在,我们可以决定是处理信息,还是警告用户错误:

if ($errorflag) {
	/* show the form again */
} else {
	/* process the form information */
}

翻译自: https://thenewcode.com/229/PHP-Form-Validation-Checking-Fields

表单验证时不验证某条字段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值