This question already has answers here:
Limiting number of lines in textarea
(8 answers)
Closed 6 years ago.
I've got a form that people can fill out and the results of the form are displayed on a page. The form is limited to 2000 characters, however there's nothing limiting how many line breaks there can be, so someone could just put one character per line and get 2000 line breaks, which clutters up the page pretty badly. I'm looking to limit the number of lines that they can type into the textarea so the limits are 2000 characters or 40 lines, whichever comes first.
I would also need this to be protective of people pasting large amounts of text into the form as well. I looked at a lot of other posts about this but none of the answers worked when I tried them. Thanks!
have you specified <textarea maxlength="2000"></textarea> - that would be a good place to start. this would limit the characters to 2000.
You could also check for the enter key (keycode=="13" if i recollect correctly) and either a) disallow it or b) only allow it after a full stop or after e.g. >30 characters. Just a suggestion
Definitely specify the maxlength though. Then no more than that will be able to be inputted. Note that breaks count as characters and are deducted from the characters remaining.
Have a go at this and you will see.
function textCount(val) {
var len = val.value.length;
if (len >= 500) {
val.value = val.value.substring(0, 2000);
$('#amtleft span').text(0);
} else {
$('#amtleft span').text(2000 - len);
}
}
textCount($('#longText').get(0));
$('#longText').keyup(function() {
textCount(this);
});
textarea{height:150px;
width:300px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea id='longText' ></textarea><br>
<span id='amtleft'><label>(<span>2000</span> characters left)</label></span>
Related
I'm new-ish to JS and regular expressions confuse me a lot so I'm struggling and have been googling for a hot minute.
I found a similar question here Regex, removing recurring characters but keeping at least one but still cannot figure this out with doing specific special characters. (/*-+.)
I'm building a calculator in javascript and trying to prevent input from looking like this if an operator button gets pressed multiple times:
6 *** 6 /// 5 ** 3...2 --- 1 ++ 9
and turn it into
6 * 6 / 5 * 3.2 - 1 + 9
instead but still allowing repeats of the operators or decimal in different places. So removing repeated characters happening multiple times in a row.
I've tried checking if the last character of the input is a repeat of the value of the operator button you press but that feels clunky.
I think a regex might be the best way to do this? Correct me if I'm wrong pls. Thank you!
Nick's comment helped! I thought I tried his solution earlier when troubleshooting but turns out I messed up the formatting before and he cleared it up! Thanks! One day I'll understand regexs. (^:
let regex = /([./*+-])\1+/;
text.textContent = text.Content.replace(regex, '$1');
Instead of replacing you can prevent the event.
document.getElementById("in").addEventListener("keypress", (evt) => {
const value = evt.target.value;
(value.indexOf("*") > -1 ||
value.indexOf("+") > -1 ||
value.indexOf("-") > -1) &&
evt.preventDefault();
});
This question already has answers here:
How to match numbers between X and Y with regexp?
(7 answers)
Closed 7 years ago.
First of all, i know Regular expressions isn't the best tool to achieve what I want here. I have done enough research to know that bit. Still, The problem I am stuck in requires me to make up a regex to find the values between some lower and upper bound values.
So here is the problem, I have a large set of data, let's say ranging between 1 and 1000000. That data is not under my direct control, I cannot manipulate the data directly. Only way of finding out (searching) some values from that data is regex.. Now, the user can give two values, a minimum value and a maximum value and I need to construct a regex based on these two values and then query the large data set using the regex to get all the values lying between the set range. So, if my data contains [1,5,7,9,15,30,45,87] and user sets the range min:10, max:40. The regex should filter out values 15, 30.
From whatever I have searched, I know it is very much possible to build a regex for finding out values between fixed values (if we know them beforehand) for example, values between 1 to 100 can be found by:
^(100|[1-9][0-9]?)$
But what gets so tricky about my problem is that the input range can be anything from pretty much 1 digit values to up to 10 digit values. 10000-550000 can be an example user input for a large data set.
I know this will require some complex logic and loops involved on the basis of number of digits in the lower bound and number of digits in the upper bound of the range and then some recursive or other magical logic to build a regex that covers all the number lying in that range.
I've been filling up pages to come up with a logic but I'm afraid it surpasses my knowledge of regex. If anyone has ever done something like this before or try to point me in the right direction or attempt it him/herself - it'll be quite helpful. Thanks.
The language I will be using this in is JavaScript and I read somewhere that JS doesn't support conditional regex, keeping that in mind, solution doesn't have to be in specific to a language.
If your task is to get numbers between min and max value from the dataset, you can try filter method.
Var filteredResults = Dataset.filter(function(item){
If(item < max && item > min)
Return item
}
)
I am working on a regular expression for Canadian phone number in javascript and / or in jQuery. I'm having some trouble into the formating after I have passed my main regular expression.
Mainly, I need to format a phone number in this way when the user leave the input field :
111-222-3333
111-222-3333 #44444 (up to 5 digits)
1-222-333-4444
1-222-333-4444 #55555 (up to 5 digits)
As you can see above, I want to be able to format a normal phone number and a toll free number at the same time.
The code
In my HTML, I have done a simple input field.
<input id="assure_telephone" placeholder="Phone number" name="assure_telephone" maxlength="25" type="text" />
For my jQuery, I picked up the code found in my large file and simplified it a little bit. We need to focus on my regular expressions.
$('#assure_telephone').bind('change', function(){
// Delete all caracters and specials caraters except numbers
telephone_user = $('#assure_telephone').val().replace(/[^0-9]/g, '');
// Format the new phone number
telephone_user_regex = telephone_user.replace(/(\d{3})(\d{3})(\d{4})(\d{0,5})/, "$1-$2-$3 #$4");
$('#assure_telephone').val(telephone_user_regex);
});
The logic behind my code
As you can see, I'm starting by removing all special caracters to only keep numbers and then after I apply a formating with the .replace() Javscript function.
Link to my actual demo : http://jsfiddle.net/y201gcdg/6/
As you can see, it is pretty obvious that a toll free won't work as my formating is really made for normal phone number and not toll free.
My question is : Is there any way to work arround with the length of my telephone_user_regex variable to detect if it is a toll free or no OR is there any way to acheive it with a better regular expression?
Alternatively, I founded this on Stackoverflow that might be helping : https://code.google.com/p/libphonenumber/ (Source: Phone number format in Javascript)
Other ressource : http://www.w3schools.com/jsref/jsref_replace.asp
EDIT#1 - Deleted it after an answer that was not usefull.
EDIT#2 - Possible answer : Count the caracters
Source : A comprehensive regex for phone number validation
I could start with my first replace() function and then count the number of caracter, if it exceed X number, then apply a formating, else do an other one.
EDIT#3 - As I did not wanted to make an answer for my own question, I will post my workarround here.
var typephone = type;
// We take out all caracters except 0 to 9
var telephone_user = $('#'+typephone).val().replace(/[^0-9]/g, '');
// Now I can make switch case to detect the kind of phone number I need to format
switch(telephone_user.length) {
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
console.log('Your phone number is too small');
break;
case 10 :
console.log('This is a phone number 111-222-3333');
break;
}
In this way, I am able to detect the length and I could be able to know if it is a toll-free or a normal phone number. I might have trouble when it will come to the point the customer wrote something like this : 111-222-3333 #44444. After the special caracter removal, I won't be able to know if it was an extension.
Here's my suggestion:
Don't replace # (i.e. do replace(/[^0-9#]/g, '')), because otherwise you couldn't detect if the 11th character is the country code or an extension.
You can then use the position of the # to determine the length of the actual number (without extension), i.e., telephone_user.search("#")
You can then conditionally format the phone number based on #2, e.g. if it's the 12th digit, format telephone_user with the country code. Also, with the switch-case, I would just do the 3-4 valid cases, and default on everything else.
This is probably a really simple thing but I can't for the life of me figure out or find anything remotely like the issue. I'm fairly new to Javascript and I'm doing some simple form validation on HTML fields. The two problems at hand are these:
if(minSquare.search(intCheck) == -1 || minSquare < 500 || minSquare > 5000 || minSquare > maxSquare)
{
errorPrint.innerHTML += "Incorrect minimum square feet amount <br />";
errors++;
}
if(maxSquare.search(intCheck) == -1 || maxSquare > 5000 || maxSquare < 500)
{
errorPrint.innerHTML += "Incorrect maximum square feet amount <br />";
errors++;
}
minSquare/maxSquare are the values taken from the HTML fields. As you can see the square feet values (in a real estate setting) are supposed to be between 500-5000 and have to be an integer. The logic is there, but every time I submit the form, the error for incorrect minimum square feet prints if the value is below 1,000 despite the lower limit being 500. This is the same case for another pair of fields in that if the value is below 100,000, it won't print despite its own lower limit being 30,000.
For some reason when I enter 500 for the minimum amount and 5000 for the maximum amount (the proper range), it will submit no problem. But if I enter say, 501 and 5000 it will throw an error for the minimum value. I have no idea why this is happening. I don't think it's a browser issue; tried on both Chrome and Firefox, same problem. Any help appreciated; the more stupid the problem the quicker I can close this up =)
EDIT: Fixed the problem. Knew it was simple. As per advice below, I just needed to parse the inputs and then I needed to get rid of the RegExp search method that would only work if the variable being searched is a string. Thanks to those who replied.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to limit number of characters per line in text area to a fixed value.
hello Friends,
I have a textarea field in my view.
I need to set per line 72 characters length.
that is user is entering more than 72 chracter per line I need go to next line.
How to set these limits using jquery or javascript?
Thanks
This is not standard, but it works in many browsers, test it.
http://www.htmlcodetutorial.com/forms/_TEXTAREA_WRAP.html
<TEXTAREA NAME="HARD" COLS="72" ROWS="5" WRAP="HARD">
Setting wrap to hard makes it send new lines to the server. Setting it to soft only breaks it visually.
No need to use javascript for this. There is a HTML attribute built into the <textarea> tag.
See some documentation here.
example for your use
<TEXTAREA NAME="HARD" COLS=72 ROWS=5 WRAP=HARD></TEXTAREA>
Using a HARD Wrap actually sets carriage returns when the line is wrapped.
I may be a bit late posting this answer, but this is best done with javaScript to ensure browser compatibility. With jQuery we can do
var count= 1;
var chars= 3;
$('#mytext').keydown(function() {
var v = $(this).val();
var vl = v.replace(/(\r\n|\n|\r)/gm,"").length;
if (parseInt(vl/count) == chars)
{
$(this).val(v + '\n');
count++;
}
});
Check working example at http://jsfiddle.net/ZWVad/2/