验证允许空格字符只有当一个字母 [英] Validation to allow space character only when followed by an alphabet
本文介绍了验证允许空格字符只有当一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想验证它有其次当任何字母字符,只允许空格字符的文本框。当插入仅一个空格字符我的code失败。什么是我的code中的错误。建议请..
I am trying to validate the textbox which has to allow space character only when followed by any alphabetic character. My code fails when only a space character is inserted. What is the mistake in my code. Suggestions pls..
的javascript:
function validate() {
var firstname = document.getElementById("FirstName");
var alpha = /^[a-zA-Z\s-, ]+$/;
if (firstname.value == "") {
alert('Please enter Name');
return false;
}
else if (!firstname.value.match(alpha)) {
alert('Invalid ');
return false;
}
else
{
return true;
}
}
查看:
@Html.TextBoxFor(m => m.FirstName, new { @class = "searchbox" })
<button type="submit" onclick="return validate();">Submit</button>
我的条件申请:
如:阿伦乔拉 - 成功的条件
如:_ - 条件失败(不应该允许单独空格字符)
Conditions I applied :
Eg: Arun Chawla - condition success
Eg: _ - condition fails (should not allow space character alone)
推荐答案
尝试以下的正则表达式
var alpha = /^[a-zA-Z-,]+(\s{0,1}[a-zA-Z-, ])*$/
第一部分强制字母字符,然后允许的空间。
First part forces an alphabetic char, and then allows space.
这篇关于验证允许空格字符只有当一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文