Html 带空格的 HTML5 表单验证模式字母数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19619428/
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 14:47:45  来源:igfitidea点击:

HTML5 form validation pattern alphanumeric with spaces?

regexhtmlvalidationformsalphanumeric

提问by RTB

I have the following input tag in my html5 form:

我的 html5 表单中有以下输入标签:

<p>
    <label>Company Name*</label>
    <input type="text" name="name" class="field" required pattern="[a-zA-Z0-9]+" />
</p>

This works just fine checking if the company name consists out of alphanumeric characters. But of course I want to allow spaces in the company name. I need to know what I should add to the pattern.

这可以很好地检查公司名称是否包含字母数字字符。但当然我想在公司名称中允许空格。我需要知道我应该在模式中添加什么。

回答by Lachezar

How about adding a space in the pattern attribute like pattern="[a-zA-Z0-9 ]+". If you want to support any kind of space try pattern="[a-zA-Z0-9\s]+"

如何在模式属性中添加一个空格,如pattern="[a-zA-Z0-9 ]+". 如果您想支持任何类型的空间,请尝试pattern="[a-zA-Z0-9\s]+"

回答by bumerang

My solution is to cover all the range of diacritics:

我的解决方案是涵盖所有范围的变音符号:

([A-z0-9à-?\s]){2,}

A-z- this is for all latin characters

A-z- 这适用于所有拉丁字符

0-9- this is for all digits

0-9- 这是所有数字

à-?- this is for all diacritics

à-?- 这适用于所有变音符号

\s- this is for spaces

\s- 这是用于空间的

{2,}- string needs to be at least 2 characters long

{2,}- 字符串至少需要 2 个字符长

回答by whitepolkydots

To avoid an input with only spaces, use: "[a-zA-Z0-9]+[a-zA-Z0-9 ]+".

为避免输入只有空格,请使用:"[a-zA-Z0-9]+[a-zA-Z0-9 ]+"

eg: abc | abc aBc | abc 123 AbC 938234

To ensure, for example, that a first AND last name are entered, use a slight variation like

例如,为了确保输入了名字和姓氏,请使用轻微的变体,例如

"[a-zA-Z]+[ ][a-zA-Z]+"

"[a-zA-Z]+[ ][a-zA-Z]+"

eg: abc def

回答by MYbuddy

It's quite an old question, but in case it could be useful for anyone, starting from a combination of good responses found here, I've ended using this pattern:

这是一个相当古老的问题,但如果它对任何人都有用,从这里找到的好回答的组合开始,我已经结束使用这种模式:

pattern="([^\s][A-z0-9à-?\s]+)"

It will require at least two characters, making sure it does not start with an empty space but allowing spaces between words, and also allowing special characters such as ?, ó, ?, ?.

它将至少需要两个字符,确保它不以空格开头,但允许单词之间有空格,并且还允许特殊字符,例如?, ó, ?, ?.

回答by ab_wanyama

Use this code to ensure the user doesn't just enter spaces but a valid name:

使用此代码可确保用户不仅输入空格,而且输入有效名称:

pattern="[a-zA-Z][a-zA-Z0-9\s]*"

回答by Selvamani P

Use Like below format code

使用像下面的格式代码

$('#title').keypress(function(event){
    //get envent value       
    var inputValue = event.which;
    // check whitespaces only.
    if(inputValue == 32){
        return true;    
    }
     // check number only.
    if(inputValue == 48 || inputValue == 49 || inputValue == 50 || inputValue == 51 || inputValue == 52 || inputValue == 53 ||  inputValue ==  54 ||  inputValue == 55 || inputValue == 56 || inputValue == 57){
        return true;
    }
    // check special char.
    if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { 
        event.preventDefault(); 
    }
})

回答by Nikhil Surkar

Use below code for HTML5 validation pattern alphanumeric without / with space :-

将以下代码用于不带 / 带空格的 HTML5 验证模式字母数字:-

for HTML5 validation pattern alphanumeric without space :-onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90"

对于没有空格的 HTML5 验证模式字母数字:- onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90"

for HTML5 validation pattern alphanumeric with space :-

用于带空格的 HTML5 验证模式字母数字:-

onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90 || event.charCode == 32"

onkeypress="return event.charCode >= 48 && event.charCode <= 57 || event.charCode >= 97 && event.charCode <= 122 || event.charCode >= 65 && event.charCode <= 90 || event .charCode == 32"