Avatar of cuconsortium
cuconsortiumFlag for United States of America

asked on 

Java Script Function

Dear all,

   Let's say I have a java script function as follow:


<script type="text/javascript">

function   format(num)
{
    bluh
    bluh
    bluh
}

</script>

   I'd like to mask an input text field data.  How do I call this format(num)  java script function in the HTML form?


</head>

<body>
<form name="DataEntryForm">
<input name="phone" type="text" id="phone" onChange="" /></form>
</body>
</html>


Thank you!!
JavaScriptAJAXJScript

Avatar of undefined
Last Comment
Suraj_Mathew
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

try here they have some input masks that you can download for free http://www.webresourcesdepot.com/javascript-input-masks/
Avatar of dr_Pitter
dr_Pitter

Hi,

try this:

function format()
{
   var num = document.getElementById('phone').value;
   bluh
   bluh
   bluh
}

<input name="phone" type="text" id="phone" onchange="format();" /></form>

Open in new window


that will trigger your function, everytime the inputfield looses its focus. If you want to trigger it everytime the value changes, use the onkeyup-event instead of onchange.
Avatar of cuconsortium
cuconsortium
Flag of United States of America image

ASKER

Hi Dr_Pitter,

  I have several text fields that need to apply this mask.  From what you described, that means I'll need to build one function for each text field.  Is there a better way?

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mr_Wizzy
Mr_Wizzy
Flag of Philippines image

hi cuconsortium,

You can set the formatting to all inputs with the same classname after loading the page by using the window.onload event.

Given you have these inputs where to set the formatting:

<input name="phone" type="text" id="phone" class="num_format" />
<input name="phone1" type="text" id="phone1" class="num_format" />
<input name="phone2" type="text" id="phone2" class="num_format" />
<input name="phone3" type="text" id="phone3" class="num_format" />

Create these javascript functions:

<script type="text/javascript">
function  format(num) {
    bluh
    bluh
    bluh
}
function setFormat()  {
      var inputs = document.getElementsByClassName('num_format');

      for( var x=0; x<inputs.length; x++ ) {
           //set the formatting
            inputs[x].onchange = function() {
                  var num = this.value;
                 
                  format(num);
            }
       }
}

window.onload = setFormat;
</script>

With that, all settings of onchange event to inputs with "num_format" class will be done after the loading of page.

Hope that helps... Have a good day... :)
SOLUTION
Avatar of Suraj_Mathew
Suraj_Mathew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo