简体   繁体   English

我表单中文本区域的最小长度

[英]Min length for text area in my form

I need to set a min length and a max length for my text area box on my website.我需要在我的网站上为我的文本区域框设置最小长度和最大长度。 I know for max i can do it in html, but min you cannot.我知道 max 我可以在 html 中做到这一点,但 min 你不能。 Could someone help me with this?有人可以帮我解决这个问题吗? I can do if, else statements but that would be after the form is submitted.我可以做 if, else 语句,但那是在提交表单之后。 I want it to tell the user the length is too short before he/she even submits it.我希望它在用户提交之前告诉用户长度太短。 I think jquery would be the best but really unsure.我认为 jquery 会是最好的,但真的不确定。

Code listed below.下面列出的代码。

        <!DOCTYPE HTML>
<html> 
<head>

<link type="text/css" rel="stylesheet" href="index.css" />
<meta name="viewport" content="width=device-width" />
<title>Daily Dorm News</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
$('form').submit(function (e) {
    var value;

    // "message" pattern : from 3 to 15 alphanumerical chars

    value = $('[name="message"]').val();
    if (!/^[A-Za-z0-9]{2,150}$/.test(value)) {
        alert('Need to enter at least 3 characters to submit this form.');
        e.preventDefault();

    }

});
});
</script>

</head>
<body>
<h1> <u>Daily Dorm News</u> <br> The best place to get your latest Dorm news </h1>
<form action="posting_wall.php" method="get">
<div id="container">
Username:<input type="text" name="name" pattern="[A-Za-z0-9]{3,15}" title="Letters and numbers only, length 3 to 15" required autofocus><br>
E-mail: <input type="email" name="email" maxlength="20" required><br>

Post:<br>
<div name="message">
<textarea rows="15" cols="50" name='message' required></textarea>
</div>
Date this event took place: <input type="text" name='date' id="datepicker" required> <br>
<input type="reset" value="Reset">
<input type="submit">

</form>

</body>
</html>

Set min-length设置最小长度

pattern=".{5,}"  --> 5 characters minimum

set both min-length and max-length设置最小长度和最大长度

pattern=".{5,100}"  --> 5 characters minimum and 100 maximum .

Example例子

<input type="text" name="test" pattern=".{20,100}" required >

to support Html5 in every browser use html5shiv在每个浏览器中支持 Html5 使用html5shiv


Updated after OP's comment OP评论后更新

$('#myform').submit(function(e){ if($('#message').val().length < 3){ e.preventdefault(); } });

Note that your HTML misses </head> .请注意,您的 HTML 缺少</head> That said, the "pattern" attribute seems to work :也就是说,“模式”属性似乎有效:

<input type="text" required="required" pattern="^[A-Za-z0-9]{3,15}$" />

Here is another way of doing this using Javascript.这是使用 Javascript 执行此操作的另一种方法。 Just put this code into the <head> section, right after $("#datepicker").datepicker();只需将此代码放入<head>部分,就在$("#datepicker").datepicker();之后:

$('form').submit(function (e) {
    var nameValue = $('[name="name"]').val();
    if (!/^[A-Za-z0-9]{3,15}$/.test(nameValue)) {
        alert('failure');
        e.preventDefault(); // prevents from submitting
    }
});

Here is a demo combining both solutions : http://jsfiddle.net/wared/KbxM8/ .这是一个结合两种解决方案的演示:http: //jsfiddle.net/wared/KbxM8/

See also : http://api.jquery.com/submit/另见: http ://api.jquery.com/submit/


I've tried something according to your comments :根据您的评论,我尝试了一些方法:

$('form').submit(function (e) {
    var value;

    // "message" pattern : from 3 to 15 alphanumerical chars

    value = $('[name="message"]').val();
    if (!/^[A-Za-z0-9]{3,15}$/.test(value.replace(/\n/g, ''))) {
        alert('Wrong value for "message".');
        e.preventDefault();
        return;
    }

    // "name" pattern : at least 1 digit

    value = $('[name="name"]').val();
    if (!/\d+/.test(value)) {
        alert('Wrong value for "name".');
        e.preventDefault();
        return;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM