admin管理员组文章数量:1421263
I have this code
var message_regex = /^.{10,500}$/;
I want the user to enter a minimum of 10
characters, But it doesn't matter how much words I enter my error code still shows up.
If I remove the 500
from the code above everything works fine. I just want to know if my regex
code is fine or am I missing something?
This is my jquery
code for the regex
if(message == ""){
$(".val_message").html("Please enter an inquiry").addClass('validate');
validation_holder = 1;
} else {
if(!message_regex.test(message)) {
$(".val_message").html("Your message is too short please enter atleast 10 characters").addClass('validate');
validation_holder = 1;
} else {
$(".val_message").html("");
}
}
Also if I put change the code to
var message_regex = /^.{5,500}$/;
It doesn't care if I enter one character. What could be wrong?
I have this code
var message_regex = /^.{10,500}$/;
I want the user to enter a minimum of 10
characters, But it doesn't matter how much words I enter my error code still shows up.
If I remove the 500
from the code above everything works fine. I just want to know if my regex
code is fine or am I missing something?
This is my jquery
code for the regex
if(message == ""){
$(".val_message").html("Please enter an inquiry").addClass('validate');
validation_holder = 1;
} else {
if(!message_regex.test(message)) {
$(".val_message").html("Your message is too short please enter atleast 10 characters").addClass('validate');
validation_holder = 1;
} else {
$(".val_message").html("");
}
}
Also if I put change the code to
var message_regex = /^.{5,500}$/;
It doesn't care if I enter one character. What could be wrong?
Share Improve this question edited Oct 5, 2013 at 9:48 andresr asked Oct 5, 2013 at 9:37 andresrandresr 933 silver badges11 bronze badges 2-
The problem could be because of new line character ....
(/^.{10,500}$/m).test('01234567800\n999')
returns false – Arun P Johny Commented Oct 5, 2013 at 9:52 - i think it has to do something with your message display is it css set to display block? – Arun Killu Commented Oct 5, 2013 at 10:08
4 Answers
Reset to default 2Do you really need regex for this. This validation can be done like this also:
// assuming ta is your textarea element
var maxlen = 500;
var minlen = 5;
if(ta.value.length > maxlen) {
$(".val_message").html("Enter at most " + maxlen + " characters in the textarea");
ta.value = ta.value.substr(0, maxlen);
}
else if(ta.value.length < minlen) {
$(".val_message").html("Enter at least " + minlen + " characters in the textarea");
}
i think the regex should look like this
var messageregex = /^[A-Za-z0-9]{10}*$/
I found my problem guys. I feel so dumb!! Probably cause it's 3 am.
<textarea id="message" class="cfMsg" name="message" placeholder="Your inquiry"></textarea>
My problem was the name="message"
I had it like name="name"
when I changed it my regex coded worked :/ Sorry! and thanks for the help I learned a new way of doing it from @anubhava
Nowadays, it would probably be easiest to use the minlength
and maxlength
attributes on the textarea
, which have been available for many years now:
<textarea
id="message"
class="cfMsg"
name="message"
minlength="10"
maxlength="500"
placeholder="Your inquiry"></textarea>
The only downside is that IE does not support minlength
, but that might not be a problem these days.
本文标签: javascriptHow to make a regex for textareaStack Overflow
版权声明:本文标题:javascript - How to make a regex for textarea? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745340319a2654231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论