File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes text input onblur calls a js function for formatting of the input data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "text input onblur calls a js function for formatting of the input data" Watch "text input onblur calls a js function for formatting of the input data" New topic
Author

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

Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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

Joined: Jan 10, 2002
Posts: 61769
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
JavaScript does not have a replaceAll, I am sure the browser's error console would point that out.

Eric
Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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

Joined: Jan 10, 2002
Posts: 61769
    
  67

Because Java is not JavaScript, and JavaScript is not Java.


Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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
 
subject: text input onblur calls a js function for formatting of the input data