使用HTML 5模式进行名字验证

5
我有这段代码。
 echo '<input type="text" maxlength="32" name="first_name" pattern="[A-Za-z]" value="'.$_SESSION['user_first_name'].'" required>';

即使我输入正确的内容,它也显示“请匹配所请求的格式”。
数据是“testme”,这是正确的,但它没有通过验证。

1
不要在2014年使用echo HTML。使用任何模板系统[Twig,Handlebars,等等]。 - moonwave99
3个回答

11

您需要指定模式的长度。pattern="[A-Za-z]{1,32}"

echo '<input type="text" maxlength="32" name="first_name" pattern="[A-Za-z]{1,32}" value="'.$_SESSION['user_first_name'].'" required>';

非常抱歉,这是我的错误。现在它已经可以工作了。谢谢。 - Raj

4

pattern="[A-Za-z ]{1,32}" <- 在名字的模式中使用空格。


0

对于数据'testme'(它有6个字符)

echo '<input type="text" maxlength="32" name="first_name" pattern="[A-Za-z]{6}" value="'.$_SESSION['user_first_name'].'" required>';

或者你可以给出一个范围的模式

echo '<input type="text" maxlength="32" name="first_name" pattern="[A-Za-z]{1,255}" value="'.$_SESSION['user_first_name'].'" required>';

在这种情况下,您可以输入1到255个字符作为输入值


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接