Regex phonenumber [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can someone help me with a regex expression for a phone number
it needs to be in this format only
xxx-xxx-xxxx

Try this
^\d{3}\-\d{3}\-\d{4}$

Multiple ways are there. For example:
var regex = /^\d{3}-\d{3}-\d{4}$/;
console.log(regex.test('999-999-9999'));
console.log(regex.test('9999-999-99999'));
//OR
var regex2 = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/
console.log(regex2.test('999-999-9999'));
console.log(regex2.test('9999-999-99999'));
You can also write specific to a country or area. See this example.
If you are taking it from user input validate it like this:
var val = document.getElementbyId('yourInputId').value;
if(regex2.test(val)){
alert("Success!!");
}
else{
alert("Failure!!");
}

Related

Check whether string includes some substring of another string - the easiest way [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Clarification - I want to check if str1 and str2 share common substring.
str1 = "and we know that the lion"
str2 = "the lion is big"
by using those two string because the lion happens to show on both string then true will be invoked.
Thanks.
You can use String.includes that will return a boolean value:
console.log("and we know that the lion".includes("the lion is big")); // returns false
console.log("and we know that the lion".includes("the lion")); // returns true

Finding a faster way of checking characters in js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to make something that gives an output based on your input. I would like it to take every letter and produce something with the next letter in the alphabet. So if I entered "Hello" it would produce "Ifmmp". The only way I can think to do this is with a series of ifs and else ifs, but I would like to know if there is a faster way.
You can use the String.fromCharCode feature. For example:
let myChar = 'c';
let nextChar = String.fromCharCode(myChar.charCodeAt(0)+1);
console.log(nextChar) // 'd'
In your case, you would replace 'c' with your character variable.

Removing last bit of a string including separator? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Let's say I have a string that looks like this:
Red#Yellow#Blue#Green
How can I use Javascript to remove the last instance of # as well as the text that comes after it, so that the resulting string would look like this:
Red#Yellow#Blue
string=string.split("#");
alert(string.pop());//Green
string=string.join("#");
I dont see a problem? Simply split by #, remove the last one and join again?
you can split the string into arrays and join all the items of the array except the last one
var myString = Red#Yellow#Blue#Green;
var myArrray = myString.split('#');
myArray.splice(myArray.length-1,1);
myArray.join('#');
console.log('Red#Yellow#Blue#Green'.replace(/\#[a-zA-Z]+$/,''));

What will be the regular expression for following date and time format like YYYYMMDD.HHMMSS.? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to validate the textbox as per date & time format like YYYYMMDD.HHMMSS so I required regular expression for date & Time using the javascript.
Please let me know the answer as soon as possible.
Required RE for YYYYMMDD.HHMMSS date Time format.
Dear, Here is your answer :
var r = /^((((19|[2-9]\d)\d{2})[\/\.-](0[13578]|1[02])[\/\.-](0[1-9]|[12]\d|3[01])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](0[13456789]|1[012])[\/\.-](0[1-9]|[12]\d|30)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((19|[2-9]\d)\d{2})[\/\.-](02)[\/\.-](0[1-9]|1\d|2[0-8])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))[\/\.-](02)[\/\.-](29)\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])))$/g;
r.test('2014-06-24 15:49:05'); // true
r.test('2016-02-29 23:19:20'); // true [leap year]
r.test('2015/02/29 23:19:20'); // false
r.test('2010.11.31 10:00:02'); // false
r.test('1956.10.12 24:10:02'); // false
Thanks & Cheers

Remove () and - and white spaces from phone number in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a phone number like (123) 456-7891. I need number like 1234567891.
How can I do that in Javascript ?
To be on the safe side, I would recommend removing everything except + (for country codes) and digits:
result = subject.replace(/[^+\d]+/g, "");
You can use String.replace() to do this. Pass it a RegExp that matches everything but digits and replace them with ''.
var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, '');
alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));
please check this link.it might help you
http://www.w3resource.com/javascript/form/phone-no-validation.php
there are many examples here.it might be useful for you.

Categories

Resources