I put 10 digit mobile numbers in a Textarea like the follwoing.
242452354643663463636346366363463636365636363634656346
but i need to put comma(,) after every 10 digit number.
Like this?
"242452354643663463636346366363463636365636363634656346".replace(/(\d{10})/g,"$1,")
// 2424523546,4366346363,6346366363,4636363656,3636363465,6346
Above solution did not help with splitting a 10 digit phone number being typed into for example a textarea.
My solution: 1. your text area should be validated to return only numbers onKeyPress.
then
function commafyPhone(str){
var newStr='';
if(str.length>10){
var str_array=str.split(",");
for(var i = 0; i < str_array.length; i++) {
newStr+=str_array[i].replace(/(\d{10})/g,'$1,');
}
return newStr;
}
return str;
}
On the textarea form field, i used:
onKeyUp="this.value=commafyPhone(this.value);"
However, my solution requires to remove the comma on your last number entered.
Related
I can input
0 but not 0123. How do I prevent users not to input 0 at the first position of the input text field?
Let them input it, just trim it when focus changes:
inputElement.onblur = function() {
this.value = this.value.replace(/^0+(?=\d)/,'');
}
Fiddle
Note that it's specifically looking for a digit (\d) after the 0's that it's nuking, so it won't touch the zero in something like 0.123. If you want to get rid of those, too, just change the \d to ., which matches any character whatsoever.
One way to do this is to check the first character in the input using charAt().
Demo
JS:
function check(){
var number = document.getElementById('input').value;
if(number.charAt(0) === 0)
alert('Leading zero in the input.')
else
alert('No leading zero in the input.')
}
EASIER SOLUTION:
For the input, just check the first character like an array:
if(number[0] === '0')
// do something
Demo
I have input as 23 digit key from input box which will be separated by '-'.
E.g: XXXXX-XXXXX-XXXXX-XXXXX
This is expected format means, 5 digit followed by -(hyphen).
Problem:
User can input any data/wrong format, like XXX-XXXXX-XXXXX-XXXXXXX, in this case index of hyphen is invalid. How can I valided the index of hyphen?
I tried:
if((prd_len==23) && (n!=-1))
{
var indices = [];
for(var i=0; i<prd_id.length;i++)
{
if (prd_id[i] === "-")
{
indices.push(i);
}
}
for(var x=0;x<indices.length;x++)
{
if((indices[x]!=5) || (indices[x]!=11) || (indices[x]!=17))
{
$('#msgErr1').text('Please enter valid key.');
flag=1;
}
}
}
where prd_len=length of the accepted input from user.
Try regular expressions
if(input.match(/^(\d{5}-){3}\d{5}$/))
everything is OK
This expression basically reads "five digits and a dash - three times, then five digits". For further reference see
http://www.regular-expressions.info/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
As thg435 said, but more human-readable :-)
var correct = input.match(/^\d\d\d\d\d-\d\d\d\d\d-\d\d\d\d\d-\d\d\d\d\d$)
Could someone help with a regular expression. I'm trying to format a phone number and handle an range of extension digits. I tried using a range [1-5], but that doesn't seem to work.
$(".phone").text(function(i, text) {
if(text.length == 10) { //this portion works fine
text = text.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
return text;
}else if (text.length > 10) { //this is where I need help
text = text.replace(/(\d{3})(\d{3})(\d{4})(\d{[1-5]})/, "($1) $2-$3 x$4");
return text;
}
});
Is there a regular expression to handle a range of numbers here?
Yes, omit the square braces and use a comma.
\d{1,5}
Your use of {[1-5]} is invalid. { and } indicate that the number of matches is between the two numbers contained within it (either parameter can be omitted), whilst [1-5] matches one character out of 1, 2, 3, 4 or 5. You need:
text = text.replace(/(\d{3})(\d{3})(\d{4})(\d{1,5})/, "($1) $2-$3 x$4");
instead. For more information, see this QuickStart on repetition.
The following regex:
x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, "-");
adds dash after each 3rd character so entered 123456789 turns into 123-456-789.
Im trying to use this regex to format phone number. The problem arises on the 10th character. So entered 1234567890 turns into 1-234-567-890.
How would I modify the above regex to turn strings that have 10 digits into 123-456-7890. I use this regex because this happens as user is typing in uses keyup event.
If you know easier or better way of doing this please help me out, dashes has to be added while user is typing in. No other characters allowed.
Notes:
Cant use Jquery Masked input plugin (because if editing the middle character it's focus gets messed up)
How about
> "12345678".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-78"
> "123456789".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-789"
> "1234567890".match(/\d{3}(?=\d{2,3})|\d+/g).join("-")
"123-456-7890"
If you ALREADY have the complete number or string
var x = "329193914";
console.log(x.replace(/(\d{3})(\d{3})(\d{3})/, "$1-$2-$3"));
If you WANT AS someone is typing...
$('#locAcct').keyup(function () {
var foo = $(this).val().split("-").join(""); // remove hyphens
if (foo.length > 0) {
foo = foo.match(new RegExp('.{1,3}', 'g')).join("-");
}
$(this).val(foo);
});
Do you need to use regular expressions for everything or would maybe something like this also help you out?
function convertToValidPhoneNumber(text) {
var result = [];
text = text.replace(/[^\d]/g,"");
while (text.length >= 6) {
result.push(text.substring(0, 3));
text = text.substring(3);
}
if(text.length > 0) result.push(text);
return result.join("-");
}
You could use this function everytime the text in your inputfield changes. It will produce the following results:
"12345678" -> "123-45678"
"123d456789" -> "123-456-789"
"123-4567-89" -> "123-456-789"
I believe the simplest way would be to add dash after every n digits would be like
var a = $('#result');
var x = "<p>asiija kasdjflaksd jflka asdkhflakjshdfk jasd flaksjdhfklasd f</p><p>12345678912345678912345678912312344545545456789</p>"
a.html(x.replace(/(\d{15})/g, "$1-"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result"></div>
Most easiest way is the following using simple javascript onkey and function... it will put dash hyphen after every 3 characters you input / type.
<input type="text" class="form-control" name="sector" id="sector" onkeyup="addDash(this)" required>
add the following script
<script>
function addDash (element) {
let ele = document.getElementById(element.id);
ele = ele.value.split('-').join(''); // Remove dash (-) if mistakenly entered.
let finalVal = ele.match(/.{1,3}/g).join('-');
document.getElementById(element.id).value = finalVal;
}
</script>
I would like to limit the substr by words and not chars. I am thinking regular expression and spaces but don't know how to pull it off.
Scenario: Limit a paragraph of words to 200 words using javascript/jQuery.
var $postBody = $postBody.substr(' ',200);
This is great but splits words in half :) Thanks ahead of time!
function trim_words(theString, numWords) {
expString = theString.split(/\s+/,numWords);
theNewString=expString.join(" ");
return theNewString;
}
if you're satisfied with a not-quite accurate solution, you could simply keep a running count on the number of space characters within the text and assume that it is equal to the number of words.
Otherwise, I would use split() on the string with " " as the delimiter and then count the size of the array that split returns.
very quick and dirty
$("#textArea").val().split(/\s/).length
I suppose you need to consider punctuation and other non-word, non-whitespace characters as well. You want 200 words, not counting whitespace and non-letter characters.
var word_count = 0;
var in_word = false;
for (var x=0; x < text.length; x++) {
if ( ... text[x] is a letter) {
if (!in_word) word_count++;
in_word = true;
} else {
in_word = false;
}
if (!in_word && word_count >= 200) ... cut the string at "x" position
}
You should also decide whether you treat digits as a word, and whether you treat single letters as a word.