File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

text input onblur calls a js function for formatting of the input data

 
Tiya Khambadkone
Ranch Hand
Posts: 67
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to execute the following code.

input Requirement : User will enter any number in the textBox. length maximum 11. may or may not contain '-'. eg: 123321123 or 12-33211-23
expected Result: the number should be formatted, displayed in the textBox as the user tabs-out of the textBox and should have '-' at the 4th and 8th position. eg : 123-321-123

I do not get the result as expected above.

<html>
<head>
<script type="text/javascript">
function formatMyId(id){
var value = window.document.getElementById(id).value;
var valueWithoutDashes = value.replaceAll("-","");
var formatted=valueWithoutDashes;
if(valueWithoutDashes.length==9){
formatted=insertAt(formatted,3,'-');
formatted=insertAt(formatted,7,'-');

}
window.document.getElementById(id).value = formatted;
}
</script>
</head>
<body>
<h1>My Id Page</h1>
<input type="text" onblur="formatMyId('myId')" id="myId" name="myId" maxlength="11" value="">
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:I am trying to execute the following code. But it is not working.


Please read ItDoesntWorkIsUseless.

What is happening? And what parts are working? Is the variable value receiving the correct element? Does valueWithoutDashes receive the expected result?

What have you done to debug the situation?
 
Tiya Khambadkone
Ranch Hand
Posts: 67
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried putting alert(value); after the following line of code.
var value = window.document.getElementById(id).value;
alert(value);

I got the expected result/alert.

But when I put alert(valueWithoutDashes); after
var valueWithoutDashes = value.replaceAll("-","");
alert(valueWithoutDashes);

I do not get any alert.

I don't know if there is any other way to debug and know the values.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaScript does not have a replaceAll, I am sure the browser's error console would point that out.

Eric
 
Tiya Khambadkone
Ranch Hand
Posts: 67
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replaceAll is a java method which can be applied on a string object. But why it does not work inside this javascript function ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64194
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Java is not JavaScript, and JavaScript is not Java.


 
Tiya Khambadkone
Ranch Hand
Posts: 67
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric, You are correct. thanks !!! I wrote my own replaceAll function inside the java script and it worked.
Bear, can you be a little polite while replying. I really appreciate your prompt reply. But a little politeness will be helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic