簡體   English   中英

PHP表單驗證編號字段

[英]PHP form validation number fields

我需要此字段僅允許將數字輸入到該字段中,並且必須在“移動電話號碼”字段中輸入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");
}`

如果您確定規則正確,則可以執行以下操作:

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

文檔

如果模式匹配給定的主題,則preg_match()返回1;如果不匹配,則返回0;如果發生錯誤,則返回FALSE。

正則表達式說明:

  • ^在字符串開頭的斷言位置
  • [0-9]匹配以下列表中存在的單個字符
  • 量詞: {11}恰好11次
  • 0-9介於0到9之間的單個字符
  • $在字符串末尾的斷言位置

您可以通過在相關輸入字段中添加以下代碼來對表單進行模式匹配,這要歸功於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/zh/function.preg-match.php

您可以通過以下方式進行驗證:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM