Link to home
Start Free TrialLog in
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!!
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

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial