javascript textarea上的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16465325/
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
Regular expression on textarea
提问by Maff
I'm having a bit of trouble validating a form I have, I can check for only letters, numbers and a full stop ("period") in a single text input, but I can't for the life of me get it to work at all on a textarea field.
我在验证我拥有的表单时遇到了一些麻烦,我只能在单个文本输入中检查字母、数字和句号(“句点”),但我终生无法获得它在 textarea 字段上工作。
in my validation I have this:
在我的验证中,我有这个:
var usernamecheck = /^[A-Za-z0-9.]{5,1000}$/;
the validation I've tried that doesn't work on the textarea ($ITSWUsers) is:
我尝试过的对 textarea ($ITSWUsers) 不起作用的验证是:
if(!document.all.ITSWUsers.value.match(usernamecheck))
{
alert ("Please write the usernames in the correct format (with a full stop between first and last name).");
return false;
}
however, the following on a 'input type="text"' works just fine on the same form
但是,“input type="text"” 上的以下内容在同一表单上工作得很好
if(!document.all.SFUsersName1.value.match(usernamecheck))
{
alert("Usernames can only contain letters, numbers and full stops (no spaces).");
return false;
}
I need it to validate usernames, 1 name per line e.g.
我需要它来验证用户名,每行 1 个名称,例如
John.smith
Peter.jones1
these are both OK but the following wouldn't be:
这些都可以,但以下不会:
John Smith
David.O'Leary
3rd.username
any help/pointers with this would be greatly appreciated (I only know basic html/php/javascript)
任何与此有关的帮助/指针将不胜感激(我只知道基本的 html/php/javascript)
采纳答案by Mike Christensen
To validate line by line, I'd use the split
function to turn each line into an array. Then, loop through the array and run your RegEx on each line. That way, you can report exactly what line is invalid. Something like this:
为了逐行验证,我将使用该split
函数将每一行转换为一个数组。然后,遍历数组并在每一行上运行您的 RegEx。这样,您就可以准确报告无效的行。像这样的东西:
<textarea id="ITSWUsers"></textarea>
<button onclick="Validate()">Validate</button>
<script>
var usernamecheck = /^[A-Za-z0-9]{5,1000}\.[A-Za-z0-9]{5,1000}$/;
function Validate()
{
var val = document.getElementById('ITSWUsers').value;
var lines = val.split('\n');
for(var i = 0; i < lines.length; i++)
{
if(!lines[i].match(usernamecheck))
{
alert ('Invalid input: ' + lines[i] + '. Please write the usernames in the correct format (with a full stop between first and last name).');
return false;
}
}
window.alert('Everything looks good!');
}
</script>
回答by MasNotsram
I'd trim the input from the textarea using JQuery (or a JS function), and then use this regex:
我会使用 JQuery(或 JS 函数)从 textarea 修剪输入,然后使用这个正则表达式:
/^([A-Za-z0-9]+\.[A-Za-z0-9]+(\r)?(\n)?)+$/
Like so:
像这样:
function testFunc()
{
var usernamecheck = /^([A-Za-z0-9]+\.[A-Za-z0-9]+(\r)?(\n)?)+$/;
if(!$.trim(document.all.ITSWUsers.value).match(usernamecheck))
{
alert ("Please write the usernames in the correct format (with a full stop between first and last name).");
return false;
}
}
<textarea id="ITSWUsers" cols="50" rows="10">
John.smith
Peter.jones1
</textarea>
<button onclick="testFunc()">Click Me</button>
See it working here:
看到它在这里工作: