telephone number format with jquery&regex done [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
telephone number format with jquery&regex
i need to verify and convert any input val into telephone number format, i.e
input er+f375g25123435s67 i need to convert into +375 25 1234567
a most suitable code is:
$('input').live({
keyup: function(){
ipt = $(this).val().replace(/[^\d]*/g, "");
// remove non-digits
ipt = "+" + ipt.substring(0, 3) + " " + ipt.substring(4, 6) + " " + ipt.substring(7, 14);
$(this).val(ipt);
}
});
but i can't enter numbers after +375
1) how to enable numbers after +375
2) how to convert ipt.substring(0, 3) + " " + ipt.substring(4, 6) + " " + ipt.substring(7, 14) into regular expression?
HERE'S AN ANSWER: http://jsfiddle.net/5UvJr/

You may possibly want to look at this: http://digitalbush.com/projects/masked-input-plugin/

The substring indexes are wrong, try this:
ipt = "+" + ipt.substring(0, 3) + " " + ipt.substring(3, 5) + " " + ipt.substring(5, 12);

here is an answer: http://jsfiddle.net/5UvJr/
$('input').live({
keyup: function(){
var phone = $(this).val().replace(/\D/g, '');
phone = phone.substring(0,12);
var myRegexp = /(\d{3})(\d{2})(\d*)/g
var mphone = myRegexp.exec(phone);
$(this).val('+' + mphone [1] + ' ' + mphone [2] + ' ' + mphone [3]);
}
});

Related

How to freeze text from moving in console.log

So I have a simple console.log script that prints this
Now when I add a letter is moves
any way to freeze it please?
code
It would probably make more sense to make all of your cells contain a space character if they are "empty". Take a look here:
var Cell_1 = "a";
var Cell_2 = " ";
var Cell_3 = " ";
var Cell_4 = " ";
var Cell_5 = " ";
var Cell_6 = " ";
var Cell_7 = " ";
var Cell_8 = " ";
var Cell_9 = " ";
console.log(
Cell_1 + "|" + Cell_2 + "|" + Cell_3 + "\n" +
Cell_5 + "|" + Cell_6 + "|" + Cell_6 + "\n" +
Cell_7 + "|" + Cell_8 + "|" + Cell_9 + "\n" +
)
This way all of your variables are the same width - one character.
For future reference, here's some code that would probably look a bit nicer:
// This is called a 2d array: essentially an array containing other arrays.
// Its good for storing grids or tables of information.
var cells = [
['a', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
// This uses Array.reduce() to generate the string.
// Google it once you feel more confident :)
console.log(
cells.reduce(
(totalString, currentRow) => totalString + currentRow.join('|') + '\n',
''
)
)
The question isn't very clear but I am assuming that you want to keep a grid aligned, the grid having multiple cells that can contain a character or not.
The problem is that the empty cells are initialised to "" (empty string) which is of size 0, but when a character is set the size will be 1, so it will shift all the following cells of 1.
An easy solution is to use a " " (space) for the empty cell instead of a "". As a result the size of a cell will always be 1 and the whole grid won't be shifted.

join() Not Removing Commas from Array

I am trying to complete a Kata whereby I create a phone number from a given array input.
Input: [1,2,3,4,5,6,7,8,9,0]
Output: (123) 456-7890
The issue I have is that once I have built my string, when I call join(''), the commas are not being removed. My results is still: (1,2,3) 4,5,6-7,8,9,0.
What is the issue with the code that is preventing this happening?
function createPhoneNumber(numbers){
var newNum = [];
newNum.push("(" + numbers.slice(0,3) + ")" + " " + numbers.slice(3,6) + "-" + numbers.slice(6,10));
return newNum.join('');
}
It sounds like the numbers parameter is an array:
function createPhoneNumber(numbers) {
var newNum = [];
newNum.push("(" + numbers.slice(0, 3) + ")" + " " + numbers.slice(3, 6) + "-" + numbers.slice(6, 10));
return newNum.join('');
}
console.log(createPhoneNumber('1234567890'.split('')));
In which case .sliceing it will produce another array, from the specified indicies, and using + with an array will result in concatenation. When the array gets implicitly turned into a string, its elements will be joined by a comma.
Join the sliced array while concatenating instead (and don't create a newNum array):
function createPhoneNumber(numbers) {
return "(" + numbers.slice(0, 3).join('') + ")" + " " + numbers.slice(3, 6).join('') + "-" + numbers.slice(6, 10).join('');
}
console.log(createPhoneNumber('1234567890'.split('')));
A nicer option would be to join the numbers into a string first:
function createPhoneNumber(numbers) {
const numStr = numbers.join('');
return `(${numStr.slice(0, 3)}) ${numStr.slice(3, 6)} - ${numStr.slice(6)}`;
}
console.log(createPhoneNumber('1234567890'.split('')));
I would recomment that you use template literals introduced in es6:
function createPhoneNumber(numbers){
const phNo = `(${numbers.slice(0,3)}) ${numbers.slice(3,6)}-${numbers.slice(6,10)}`
return phNo
}
createPhoneNumber('1234567890') // output ==> (123) 456-7890

How to had space when concatenation a JavaScript sentence [duplicate]

This question already has answers here:
Joining two strings with a comma and space between them
(9 answers)
Closed 3 years ago.
I am inserting specific word into a sentence with concatenation but the words a running together with no space between them.
var adjective1 = + 'amazing';
var adjective2 = + 'fun';
var adjective3 = + 'entertaining';
var madLib = 'The Intro to JavaScript course is' + adjective1 + 'james and Julia are so' +
adjective2 + 'I cannot wait to work through the rest of this' + adjective3 + 'content!';
console.log(madLib);
Just add spaces
var madLib = 'The Intro to JavaScript course is ' + adjective1 + ' james and Julia are so' +
adjective2 + 'I cannot wait to work through the rest of this ' + adjective3 + ' content!';
You also shouldn't have + before the strings in the variable assignments. That will try to convert the strings to numbers, and will produce NaN since they're not numeric.
var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';

Why does regex not match this string

I am writing following regex for validating Different currency entries in excel sheet format:
/^((\s*[\$]?[+-]?\d*|(\d{0,3}(\s?\d{3})*)(,\d+)?[\$]?(\s*[sSrR]\s*(\d+|(\d+-\d+)))?)|((\s*[\$]?[+-]?\d*|(\d{0,3}(\s?\d{3})*)(,\d+)?[\$]?(\t\s*[\$]?[+-]?\d*|(\d{0,3}(\s?\d{3})*)(,\d+)?[\$]?)*[\n\rx]*)+))$/
This regex is generated by following javascript code:
var groupingSeparator = "\\s"; // For euro currency
var decimalSeparator = ","; // for euro currency
var NUMBER = "\\s*[\\$]?[+-]?\\d*|(\\d{0,3}(" + groupingSeparator + "?\\d{3})*)(" + decimalSeparator + "\\d+)?[\\$]?";
this.basicNumberRegExp = NUMBER;
var REPEAT_NUMBER = NUMBER + "(\\s*[sSrR]\\s*(\\d+|(\\d+-\\d+)))?";
var EXCEL_LINE = NUMBER + "(\\t" + NUMBER + ")*";
var EXCEL_MULTI_LINE = "(" + EXCEL_LINE + "[\\n\\rx]*)+";
var ret = "^((" + REPEAT_NUMBER + ")|(" + EXCEL_MULTI_LINE + "))$";
It should validate following entries:
3r3
-3r3
333
-333
333,33
-333,33
333 333
-333 33
But it is not matching/validating the following value, What is the reason:
-3r3
You missed a pair of parentheses.
You defined
var NUMBER = "\\s*[\\$]?[+-]?\\d*|(\\d{0,3}(" + groupingSeparator + "?\\d{3})*)(" + decimalSeparator + "\\d+)?[\\$]?";
Too long, but it's basically
var NUMBER = "(...) | (...)";
In the statement
var REPEAT_NUMBER = NUMBER + "(\\s*[sSrR]\\s*(\\d+|(\\d+-\\d+)))?";
, REPEAT_NUMBER is defined as
"(...) | (...)(...)?"
which is parsed as (because | has a lower precedence than concatenation)
"(...) | ((...)(...)?)"
Adding a pair of parentheses will work. Define
var NUMBER = "(\\s*[\\$]?[+-]?\\d*|(\\d{0,3}(" + groupingSeparator + "?\\d{3})*)(" + decimalSeparator + "\\d+)?[\\$]?)";
Try it online!

how to make a counter that is 4 digit spaces long? [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Unique ID that gets a date in specific format?
(2 answers)
Closed 8 years ago.
I have a generate id button, when I click on the button it generates the date '2013-06-13' in a textbox. I want to add a 4 digit counter to the end of it so it will look like this '2013-06-13-xxxx' (0001, 0002, etc). When I click on the the generate button I want it to show '2013-06-13-0001' then I will push a submit button and the form will reset. When I push the generate id button again I want it to show '2013-06-13-0002' what code may I use to do this?
var counter = 0000;
function Counter() {
if((document.getElementById("generateid").clicked == true)
{
Counter++
return counter;
}
}
This is the output code
function guidGenerator() {
var theID = (Year() + "-" + Month() + "-" + Day() + "-" + Counter);
return theID;
}
You can use this:
var str = '000' + Counter();
var result = str.substring(str.length - 4);
var theID = (Year() + "-" + Month() + "-" + Day() + "-" + result);
I think here is what you looking for How to output integers with leading zeros in JavaScript
like this function below
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length-size);
}
and actually you just need s = "000" + num;

Categories

Resources