I am validating a form using angularjs and need to write custom directive for validating usa telephone format(111-111-1111) .When user leaves the telephone text box and if matches the pattern then it automatically show as 3 chunks like above.And also when I retrieve data from server it should display as 3 chunks.I need sample code using angularjs.
Hi, @ksd07 can you please paste what you need in here:
_ http://jsfiddle.net/.
In my opinion, i think you should have a function use regex to check out your telephone format. Below is my code:
var re = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
var str = '123-123-1234';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
Test pattern in here: http://regex101.com/.
First: create a function in factory or external function.
Second: bind event after you submit or typing. Using regex to check if it is avaiable.
Third: if the format is avaiable get data from server by calling a factory handle getting data.