[英]Match Regular Expression in Javascript
I am using Javascript to validate my form fields, and the below regular expression is not working. 我正在使用Javascript验证我的表单字段,并且以下正则表达式不起作用。 Am I using the wrong method to match? 我使用错误的方法进行配对吗?
var name=document.getElementById('first_name').value;
if (!name.match(/^[A-Za-z]*$/))
{
document.getElementById('error').innerHTML = "Please match the regxp";
document.getElementById('first_name').focus();
return false;
}
i am executing the above code on form submit. 我正在表单提交上执行以上代码。
The *
repetition operator matches zero or more occurrences, so the regular expression ^[A-Za-z]*$
will match zero or more occurrences of a letter, ie it will match the empty string as well as a string containing only letters. *
重复运算符匹配零个或多个出现,因此正则表达式^[A-Za-z]*$
将匹配零个或多个出现的字母,即它将匹配空字符串以及仅包含字母的字符串。
You probably meant to use the +
repetition operator to match 1 or more like ^[A-Za-z]+$
您可能打算使用+
重复运算符来匹配1个或多个类似^[A-Za-z]+$
Try this. 尝试这个。 Using Regex would do that. 使用Regex可以做到这一点。
function validateName(x){
var name=document.getElementById('first_name').value;
var pattern = new RegExp('/^\w+$/');
if(!pattern.test(name)){
alert("Use only A-Z, a-z, 0-9, or _ in names.");
return false;
}
}
I'm not sure your problem is in the code you posted, is that bound properly to an event handler? 我不确定您的问题出在您发布的代码中,是否正确绑定到事件处理程序?
It works fine in jsfiddle http://jsfiddle.net/dD6y3/ 它在jsfiddle中正常工作http://jsfiddle.net/dD6y3/
Just added on blur binding, maybe it's what you are missing? 只是添加了模糊绑定,也许正是您所缺少的? or what is wrong? 还是什么错? Hard to say without seeing more code. 不看更多代码就很难说。
$('#first_name').on("blur", function() {
var name = document.getElementById('first_name').value;
if (!name.match(/^[A-Za-z]*$/)) {
document.getElementById('error').innerHTML="Please match the regxp";
document.getElementById('first_name').focus();
return false;
}
});
Your regular expression seems to be ok, have you attached the event to the input? 您的正则表达式似乎还可以,您是否已将事件附加到输入中? You should add the event so as to check whether it's correct or not: http://www.w3schools.com/js/js_htmldom_events.asp 您应该添加事件以便检查它是否正确: http : //www.w3schools.com/js/js_htmldom_events.asp
Here's my solution, it checks the error when you deselect the input, when you change to another input: 这是我的解决方案,当您取消选择输入时,当您更改为另一个输入时,它将检查错误:
HTML 的HTML
<div id='error'></div>
<input id='first_name' onchange="checkChanges()"/>
Although this will work, you should use Event Handlers or a framerowk such as jQuery so as to guarantee it'll work on all possible browsers 尽管这可以工作,但是您应该使用事件处理程序或jQuery之类的framerowk,以确保它可以在所有可能的浏览器上使用
JS JS
function checkChanges() {
var name = document.getElementById('first_name').value;
if (!name.match(/^[A-Za-z]*$/))
{
document.getElementById('error').innerHTML="Please match the regxp";
document.getElementById('first_name').focus();
return false;
} else {
document.getElementById('error').innerHTML="OK";
}
}
Assuming you want your name to begin with a letter, and you're not using jQuery... 假设您希望您的名字以字母开头,并且您没有使用jQuery ...
var errors = document.getElementById('error');
var firstName = document.getElementById('first_name');
function validate(element) {
var name = element.value;
if (name.match(/^[A-Za-z]+$/)) {
return true;
}
errors.innerHTML = "Name must begin with a letter";
element.focus();
return false;
}
validate(firstName); // needs moving to a submit event handler or similar
I have solved it... 我已经解决了...
To match a regular expression, I used the below method... 为了匹配正则表达式,我使用了以下方法...
var pattern=/^[A-Za-z0-9]+$/;
if (!pattern.test(document.getElementById('first_name').value))
{
document.getElementById('error').innerHTML = "only the characters and 0-9 are allowed";
document.getElementById('first_name').focus();
return false;
}
By the way, thanks for all your reply 顺便说一句,谢谢你的答复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.