Mask a input in HTML using javascript [duplicate] - javascript

This question already has answers here:
Mask US phone number string with JavaScript
(9 answers)
Closed 7 years ago.
How can I mask a HTML input text with a mask for phone number?
Here in Brazil, in some states, the number format can change.
For example:
(dd)xxxx-xxxx
this is the default number format, where dd is the state code, and the x's are the numbers.
BUT, in some states, the number nine is added to the phone number, like this:
(dd)9xxxx-xxxx. (let's call it "new format")
the var dd isn't important, what I want to do is when the user type the new format, the hyphen automatically appears after the 4th X.

When the users start entering the phone number, you could have a regex such as ^(\d{2})9 and regex match the string to see if the hyphen needs to be added at a different place.

You could use something like formatter.js to do the job for you!
I have not tried it, but the demos look quite solid.

Try using two inputs with different masking (8 digit & 9 digit respectively) and switch them based on the state selected in the form.

Related

Auto Reg Ex Phone Number format (xxx) xxx-xxxx [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I found a pretty old example but it's almost doing exactly what i need to do:
Auto Dash using Javascript on FOCUS (for a telephone number format)
However instead of the current format xxx-xxx-xxxx i'd like to do (xxx) xxx-xxxx
Can someone give me a hand with the script below with this??
$('.telnumber').keyup(function() {
this.value = this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
console.log("this value", this.value);
});
You could do something like this
var a = this.value.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1);
this.value = ['(',a[0],') ',a[1],'-',a[2]].join('');
It's not very elegant but hey, elegance is for tailors right?
Update
Yea, I got bored and made a one-liner, here:
this.value = '('+this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1)
.map((a,i)=>(a+[') ','-',''][i])).join('');
The code you'll want to change is .join('-'). That's where you're getting the 'xxx-xxx-xxxx' from.
You need to tokenize those 3 regular expressions and then join them separately.

How to split country code and phone number in java script [duplicate]

This question already has answers here:
How to split mobile number into country code, area code and local number?
(7 answers)
Closed 7 years ago.
I have a number example this : - +91xxxxxxxxxx i want to split country code and mobile number.
The number is stored in a variable. and i want to split the number from end. because i am only using those country whose phone number is 10 digit long. So i can count from last and split after 10 numbers. But how to do this. I don't know.
please help.
You can try this:
const phoneNumber = '+91xxxxxxxxxx';
const countryCodeLength = phoneNumber.length - 10;
const components = {
IDDCC: phoneNumber.substr(0, countryCodeLength),
NN: phoneNumber.substr(countryCodeLength, phoneNumber.length)
};
console.log(components);
Country may have variable length code. So best possible solution would be start from back as of now we know length of mobile number is 10. once you take those 10 digits, Then you can parse country code accordingly.

Javascript telephone number validation [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Phone Number Validation Javascript
Hi I am creating validation for a form using javascript.
I am wanting to check the telephone number field using if statements to ensure it contains a 4 digit area code then a space followed by a 7 digit number.
Any help would be appreciated.
Thanks
So you need to validate something like 1234 1234567
function isValidTel(numberToTest){
var pattern =/^\d{4} \d{7}$/
return pattern.test(numberToTest);
}

How to Apply input Mask on a Textbox

I have a textbox on which I apply input masks.
I have downloaded JS library from here link to js Library with bit documentation
What I need to do is that I need format like this
any length integer/any length characters/CONSTANT lets say = hello/
current year lets say = 2012
that is 6666/Edward/thanks SO/2012
if var lenght is not possible than atleast one integer or one character and atmost 15 integer or character
Maybe this - http://jsfiddle.net/ZYH9f/
It's one mandatory integer/character and 5 optional others:
$("input").mask("*?*****/*?*****/*?*****/9999");

comparing dates in Javascript - Results not consistent [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to check 2 date fields and compare to see which date is ahead, behind or the same
I am trying to implement a validation which should compare two dates and give an alert message.
entrydate is a text feild in our ASP page and so is vdata. I should check and make sure that vdata is always greater than or equal to entrydate. The code below is not working.
Please help to identify what the problem is with the this code:
if(document.Step2.entrydate.value <= document.all(vData).value)
The problem is that the text in an input box is just that, text. You are trying to compare dates, so you will need to convert those strings into dates and compare the dates.
The problem is that the value of a textfield is a string. So you're basically comparing strings, and not dates.
You would first need to parse your string into an actual Date object before you can do a reliable comparison. How you would do that depends on the format of the data

Categories

Resources