验证输入
问题描述:
我想验证用户名只作为数字(php或javascript)。我所做的验证接受数字的数字。 此外,我想验证的电话号码输入为+961-00/000 000
(2digits/6digits)(PHP或JavaScript)验证输入
这是我的PHP代码:
function validate_username($field) {
if (preg_match("/^[0-9]+$/",$field))
return "Only numbers";
return "";
}
<th>
Id:
</th>
<td>
<input type="text" name="username" required=""
placeholder="ID"/>
<?php if ($_SESSION['user_exists'] !== "ok")
echo "<div style='color:red;'>ID Already
exists</div>";
echo "<div style='color:red;'>$userv</div>" ?>
</div>
</td>
<th>
Phone number:
</th>
<td>
<input type="text" name="phonenumber" required=""
placeholder="+961 --/--- ---"/>
</td>
答
最好的办法是从服务器端做( javascript可以由用户修改)。
你用is_numeric尝试过吗?
$tel = "hello world";
if(!is_numeric($tel)) {
echo "$tel isn't numeric";
}
+0
是的,但它没有工作 –
+0
什么是错误? –
究竟是不是在这里工作? – Maximus2012
对于电话号码验证,你可以使用这个:https://regex101.com/r/wQ2mF4/1(你也有代码生成器按钮,非常漂亮的regex101功能) – sinisake