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. Could someone help me with this? I can do if, else statements but that would be after the form is submitted. 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.
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
$('#myform').submit(function(e){ if($('#message').val().length < 3){ e.preventdefault(); } });
Note that your HTML misses </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. Just put this code into the <head>
section, right after $("#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/ .
See also : 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;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.