简体   繁体   English

PHP检查ctype并在单个if语句中strlen

[英]PHP Checking ctype and strlen in single if statement

I have a form that validates First Name, Last Name, and E-mail before processing. 我有一个在处理之前验证名字,姓氏和电子邮件的表格。 It seems however that something isn't working. 但是,似乎有些东西不起作用。 If I enter an e-mail address and my script validates it, then the page continues regardless of the checks I have in place for First and Last Name. 如果我输入了电子邮件地址,并且我的脚本对其进行了验证,则无论我是否检查“姓氏”和“姓氏”,页面都会继续。

HTML Form Inputs HTML表单输入

First Name <input type="text" id="new_user_fn" name="new_user_fn">
Last Name <input type="text" id="new_user_ln" name="new_user_ln">

PHP Script PHP脚本

$ec = 0;
$fn = trim($_POST['new_user_fn']);
$ln = trim($_POST['new_user_ln']);

if(!ctype_alpha($fn) OR strlen($fn) <= 0){
    $error = "First Name cannot be blank and may only contain letters";
    $ec++;
}

if(!ctype_alpha($ln) OR strlen($ln) <= 0){
    $error = "Last Name cannot be blank and may only contain letters";
    $ec++;
}

Essentially this checks if the $fn is alphabetic and greater than 0 in length to ensure the user filled out the input with valid text. 实质上,这检查$ fn是否为字母且长度大于0,以确保用户用有效文本填写输入。 I go on to verify the e-mail address (this part works correctly so it has been omitted from the post) and check the value of $ec. 我继续验证电子邮件地址(此部分工作正常,因此已从帖子中省略了)并检查$ ec的值。

if($ec > 0){
    die($error);
}else{
    // Finish Processing
}

So if I leave the inputs for "fn" and "ln" blank but supply a valid e-mail address, the script still executes as if $ec = 0. 因此,如果我将“ fn”和“ ln”的输入留空,但提供有效的电子邮件地址,则脚本仍将执行,好像$ ec = 0。

Am I missing something simple? 我是否缺少简单的东西?

Lukas found the issue above. 卢卡斯找到了上面的问题。 Another $ec = 0 existed that was not commented out. 存在另一个未注释掉的$ ec = 0。 Removing it solved the problem. 删除它解决了问题。 Thanks all. 谢谢大家 Always nice to have an extra set of eyes sometimes. 有时多留一双眼睛总是很高兴。 Much appreciated! 非常感激!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM