Skip Navigation

[Resolved] Javascript not working on phone numbers

This support ticket is created 5 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Author
Posts
#1241279

I am trying to: I'm trying to add a java-script in order to add the parentheses and dashes on phone numbers on the forms.

Here is the code:

jQuery(function($) {
$('.celular').on('keypress', function(e) {
  var key = e.charCode || e.keyCode || 0;
  var phone = $(this);
  if (phone.val().length === 0) {
    phone.val(phone.val() + '(');
  }
  // Auto-format- do not expose the mask as the user begins to type
  if (key !== 8 && key !== 9) {
    if (phone.val().length === 3) {
      phone.val(phone.val() + ')');
    }
    if (phone.val().length === 4) {
      phone.val(phone.val() + ' ');
    }
    if (phone.val().length === 6) {
      phone.val(phone.val() + '-');
    }
    if (phone.val().length === 11) {
      phone.val(phone.val() + '-');
    }
    if (phone.val().length >= 16) {
      phone.val(phone.val().slice(0, 13));
    }
  }

  // Allow numeric (and tab, backspace, delete) keys only
  return (key == 8 ||
    key == 9 ||
    key == 46 ||
    (key >= 48 && key <= 57) ||
    (key >= 96 && key <= 105));
})

.on('focus', function() {
  phone = $(this);

  if (phone.val().length === 0) {
    phone.val('(');
  } else {
    var val = phone.val();
    phone.val('').val(val); // Ensure cursor remains at the end
  }
})

.on('blur', function() {
  $phone = $(this);

  if ($phone.val() === '(') {
    $phone.val('');
  }
});
});

It's working on this site: hidden link

I also added the word "celular" to the class field here:

<div class="form-group col-sm-3 celular">
			<label>Celular</label>
			[cred_field field='celular' force_type='field' class='form-control' output='bootstrap']
		</div>

Not sure why it's not working.

#1241280

I should have added the "celular" inside the cred shortcode. My bad. My issue is resolved now. Thank you!