Java Script Function

cuconsortium
cuconsortium used Ask the Experts™
on
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!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try here they have some input masks that you can download for free http://www.webresourcesdepot.com/javascript-input-masks/
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.

Author

Commented:
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!
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Multitechnician
Top Expert 2014
Commented:
Using "this" keyword as parameter :
<input name="phone" type="text" id="phone" onchange="format(this);" /></form>

Open in new window


You receive a reference on the object

function   format(which)
{
    var num = which.value;
    bluh
    bluh
    bluh
}

Open in new window

Commented:
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... :)
As leakim971 had said , generalize the format() method , so that you can handle n number of text fields........

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial