• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Java is not JavaScript, and JavaScript is not Java.


 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic