Solved

validation and formatting using custom directive with angularjs

Posted on 2014-10-15
4
170 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
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Animate a Title 7 33
Detecting robots? 5 32
Cannot scroll down on html page 2 25
jqplot Pie Chart - javascript 2 6
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now