使用 html 5 模式的名字验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25184310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:26:50  来源:igfitidea点击:

First name validation using html 5 pattern

htmlvalidationdesign-patterns

提问by Raj

I have this code

我有这个代码

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

Even if I put something correct it says 'PLease match the requested format'

即使我输入正确,它也会显示“请匹配请求的格式”

The data is 'testme' which is correct but it's not passing the validation.

数据是“testme”,这是正确的,但没有通过验证。

回答by user2687506

You need to specify how long the pattern should be. pattern="[A-Za-z]{1,32}"

您需要指定模式的长度。 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>';

回答by Vinoth

pattern="[A-Za-z ]{1,32}"<- Use space in the first name pattern.

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

回答by Anuxxule

For the data 'testme' (it has 6 characters)

对于数据“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]{6}" value="'.$_SESSION['user_first_name'].'" required>';

or else you can give a pattern with a range

否则你可以给出一个范围的模式

echo '<input type="text" maxlength="32" name="first_name" pattern="[A-Za-z]{1,255}" 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>';

in this case you can input 1 to 255 characters as input value

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