【问题标题】:PHP form validation number fieldsPHP 表单验证号码字段
【发布时间】:2016-04-19 15:59:06
【问题描述】:

我需要这个字段只允许输入数字,并且必须有11位数字输入到“手机号码”字段中,这是我创建的表格

<form name="myForm" id="userDetails" action="formProcess.php" onSubmit="return validateForm()" method="post">
  <fieldset class="custInf">
    <h3> User Details </h3>

    <label class="inputArea" for="fName">Forename :</label>
    <input type="text" name="forename" id="fname" placeholder="Enter First Name" maxlength="20" size="15"></input>

    <label class="inputArea" for="sName">Surname :</label>
    <input type="text" name="surname" id="surname" placeholder="Enter Last Name" maxlength="20" size="15"></input>

    <label class="inputArea" for="email">Email :</label>
    <input type="email" name="email" id="email" placeholder="Enter Email Address" maxlength="40" size="15" /></input>

    <label class="inputArea" for="hmph">Landline number :</label>
    <input type="tel" name="landLineTelNo" id="hmphone" placeholder="Enter Landline no." maxlength="11" size="15"></input>

    <label class="inputArea" for=" mobileTelNo">Mobile number :</label>
    <input type="tel" name=" mobileTelNo" id="mobile" placeholder="Enter Mobile no." maxlength="11" size="15"></input>

    <label class="inputArea" for="address">Postal Address :</label>
    <input type="text" name="postalAddress" id="address" placeholder="Enter House no." maxlength="25" size="15"></input>
  </fieldset>

  <fieldset class="contactType">
    <h3>
	How would you like to be contacted?
	</h3>
    <label class="radioBt" for="sms">SMS</label>
    <input id="smsBut" type="radio" name="sendMethod" value="SMS"></input>

    <label class="radioBt" for="email">Email</label>
    <input type="radio" name="sendMethod" value="Email"></input>

    <label class="radioBt" for="post">Post</label>
    <input type="radio" name="sendMethod" value="Post"></input>
  </fieldset>

  <fieldset class="termsCon">
    <input type="checkbox" name="check" value="check" id="check" />I have read and agree to the Terms and Conditions and Privacy Policy</input>
  </fieldset>

  <input class="submitBut" type="submit" name="submitBut" value="Submit" </input>

这是我迄今为止创建的用于验证用户输入的 PHP `//确保用户输入所需的信息

$fname = $_REQUEST['forename'];
if (empty($fname)) {
die("<p>Enter a first name</p>\n");
}

$surname = $_REQUEST['surname'];
if (empty($surname)) {
die("<p>You must enter a surname</p>\n");
}

$email = $_REQUEST['email'];
if (empty($email)) {
die("<p>You need to enter an email</p>\n");
}

$hmphone = isset($_REQUEST['hmph']) ? $_REQUEST['hmph'] : null ;

$mobile = $_REQUEST['mobileTelNo'];
if (empty($mobile)) {
die("<p>Enter a Mobile Number</p>\n");
}

$address = $_REQUEST['postalAddress'];
if (empty($address)) {
die("<p>Enter your postal address</p>\n");
}

$sendMethod = $_REQUEST['sendMethod'];
if (empty($sendMethod)) {
die("<p>Please choose a contact option</p>\n");
}

$check = $_REQUEST['check'];
if (empty($check)) {
die("<p>Please select the Terms and Conditions to continue</p>\n");
}`

【问题讨论】:

  • javascript 是验证表单的常用方法
  • “必须有 11 位数字” 我猜你永远不想与北美以外的国家合作?
  • 你必须,不,让那个总是验证服务器端。 @米海
  • 使用 html 5 patern w3schools.com/tags/att_input_pattern.asp 和 maxlenght w3schools.com/tags/att_input_maxlength.asp 然后可以用js函数和php函数再次验证
  • @JorgeMejia 第二个示例允许使用数字以外的字符且少于 11 个字符。但是我仍然同意 Jay Blanchard 的观点,即您必须验证服务器端。

标签: php html validation


【解决方案1】:

借助 html5 模式标签,您可以通过在相关输入字段中添加以下代码来在表单内进行模式匹配。有关模式标签的更多详细信息:http://www.w3schools.com/tags/att_input_pattern.asp

pattern="[0-9]{11}"

谢天谢地,在服务器端 php 检查非常相似!

$isValid = preg_match("/[0-9]{11}/", $formvalue);

$isValid 如果匹配则返回 true (1),如果不匹配则返回 false (0)。

preg_match 通常对于自定义验证来说是一个非常有用的函数。这里有更多详细信息:http://php.net/manual/en/function.preg-match.php

【讨论】:

    【解决方案2】:

    如果你确定你的规则是正确的,你可以这样做:

    if (empty($mobile) || preg_match('/^[0-9]{11}$/', $mobile) != 1 ) {
        die("<p>Enter a Mobile Number</p>\n");
    }
    

    来自docs

    preg_match() 如果模式匹配给定的主题,则返回 1,如果不匹配,则返回 0,如果发生错误,则返回 FALSE。

    正则表达式解释:

    • ^ 断言字符串开头的位置
    • [0-9] 匹配下面列表中的单个字符
    • 量词:{11} 正好 11 次
    • 0-9 0 到 9 范围内的单个字符
    • $ 在字符串末尾断言位置

    【讨论】:

    • 即使这不是 OP 要求的,我也会做类似$mobile = preg_replace('/[^\d]/', '', $_POST['mobile']); 的事情,并允许他们以任何他们想要的方式输入。因此,如果他们这样做+1 (123) 555-5555,它仍然会正确匹配。只需确保字符串中某处共有 11 位数字即可。
    • 这个工作了,谢谢伙计:)我还没有检查过其他人,但是这个工作
    • 我同意@Mike,我会更“宽容”。
    • 实际上在应用代码时,每次我提交表单时,我都会收到移动错误消息...有什么建议吗?
    • 我应用了一个或两个 @OliverQueen 的编辑,你都得到了吗?
    【解决方案3】:

    你可以用这个来验证:

    if(empty($number)) {
        echo '<p> Please enter a value</p>';
    } else if(!is_numeric($number)) {
        echo '<p> Data entered was not numeric</p>';
    } else if(strlen($number) != 11) {
        echo '<p> The number entered was not 6 digits long</p>';
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      相关资源
      最近更新 更多