?
Solved

validation and formatting using custom directive with angularjs

Posted on 2014-10-15
4
Medium Priority
?
181 Views
Last Modified: 2014-11-08
I am writing custom directive to validate and format the input using angularjs and I stuck in the middle while formatting the input.The valid input must have 3 digits with 2 decimals.Below I have mentioned 3 regex patterns with output format and if user enters any one of the below matching regex pattern and it should format accordingly.
In the directive I am using blur function because I want format/show error message when user leaves the textbox.Please help me to fix this issue.Here is the fiddle.fiddle


            input with reg pattern             output(need to format)

      Ex:      // [/^\d{3}$/] 789  --->               789.00
              // [/^(\d{3})\.(\d{1})$/] 789.4  --->  789.40
                // [/^(\d{3})(\d{2})$/] 78945-->       789.45
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40381874
why a regex?

if(inputvalue) {
   return ((inputvalue.substr(0,3) + "." + inputvalue.substr(3) ) * 1).toFixed(2);
}
return undefined;

Open in new window

0
 

Author Comment

by:ksd123
ID: 40382750
Thank you.I have updated the directive with the above code.It is not working as expected,sometimes I am getting NaN ,if I enter 10 digits it's formatting and giving first 5 digits with 2 decimals which is not valid as it show error message.In  one scenario it is working as expected ie. if user enters 123, it gives 123.00.
Here is the updated fiddle directive.Can you correct me what went wrong?
Below is my requirement
Invalid input
12346789-invalid format
Qswe1234-invalid format

If user enters valid input then it should format
Valid input
123 ---> 123.00
123.4--->123.40
12345--->123.45
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 40383226
test page : http://jsfiddle.net/4HRJu/12/
                   
                     if(inputvalue) {
                         var x = inputvalue;
                         while(x>999) x = x / 10;
                         x = (x+"").split(".");
                         if(x.length<2) x.push("0");
      return ((x[0] + "." + x[1] ) * 1).toFixed(2);

Open in new window

0
 

Author Comment

by:ksd123
ID: 40383518
Thank you so much,It's working fine in following scenarios
123 ---> 123.00
123.4--->123.40
12345--->123.45

But still it is failing in  following scenarios and want to show expected output (error message) for below scenarios.The valid input must have 3 digits with 2 decimals

i/p                actual o/p     expected o/p
123456-->        123.46             Invalid Format error message
1234567-->      123.46             Invalid Format error message
12            -->      12.00               Invalid Format error message
1              ->         1.00                Invalid Format error message
abcdef    -->     NaN                Invalid Format error message
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question