PHP表单验证号字段

时间:2016-04-19 15:59:06

标签: php html validation

我需要此字段仅允许将数字输入到字段中,并且必须有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>
&#13;
&#13;
&#13;

这是我到目前为止创建的用于验证用户输入的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");
}`

3 个答案:

答案 0 :(得分:3)

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

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之间范围内的单个字符
  • $断言字符串末尾的位置

答案 1 :(得分:1)

由于html5模式标记,您可以在相关输入字段中添加以下代码,从而在表单内进行模式匹配。有关模式标记的更多详细信息,请参见:http://www.w3schools.com/tags/att_input_pattern.asp

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

服务器端的php检查非常相似谢谢!

@if($errors->has())
    <div class="alert alert-danger">
        @foreach ($errors->all() as $error)
            <div>{{ $error }}</div>
        @endforeach
    </div>
@endif

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

preg_match是一般用于自定义验证的非常有用的函数。有关详细信息,请访问:http://php.net/manual/en/function.preg-match.php

答案 2 :(得分:0)

你可以用这个验证:

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>';
}