This topic contains a post which is marked as Best Answer. Press here if you would like to see it.
*

siken

  • ****
  • 138 posts
I need the code to remove the spaces and symbols that users put when writing their phone, in the s_phone field of itempost.php

*

MB Themes

Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #1 on: May 08, 2022, 09:38:10 AM »
It is taken from country settings if you mean country prefix (ie +91)
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

siken

  • ****
  • 138 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #2 on: May 09, 2022, 11:19:21 PM »
I don't want to add prefixes, I want that if a user adds spaces or symbols in the phone field, they are deleted or an error is output saying that only numbers are allowed and not symbols or spaces

*

MB Themes

Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #3 on: May 10, 2022, 04:04:59 PM »
@siken
Best way to achieve that is to use some kind of jQuery validation, or simply change field type to number.

Found this one:
Code: [Select]
$('input[name="sPhone"]').keyup(function(e)
                                {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

siken

  • ****
  • 138 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #4 on: May 11, 2022, 01:32:23 AM »
I have added it in itempost.php but it allows to add spaces and letters in the telephone field

Marked as best answer by leales_org on May 16, 2022, 09:23:06 AM
*

mwindey

  • *****
  • 485 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #5 on: May 11, 2022, 10:55:04 AM »
@siken,

I tested the answer from MB themes and works correct.... only nr no letters allowed.
Maybe you have putted it in the wrong place? This is how it works in veronika
Code: [Select]
         
<div class="row">  <label for="phone"><?php _e('Mobile Phone''veronika'); ?><?php if(strpos($required_fields'phone') !== false) { ?><span class="req">*</span><?php ?></label>
<div class="input-box"><input type="tel" id="sPhone" name="sPhone" value="<?php echo $prepare['s_phone']; ?>" /><i class="fa fa-phone"></i></div>
<script>
 $('input[name="sPhone"]').keyup(function(e)                              {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});
</script>

To allow 1 symbol like i.e: +91 use following:
Code: [Select]
<script>
 $('input[name="sPhone"]').on("input", function(evt) {
   var self = $(this);
   self.val(self.val().replace(/[^0-9\+]/g, ''));
   if ((evt.which != 46 || self.val().indexOf('+') != -1) && (evt.which < 48 || evt.which > 57))
   {
     evt.preventDefault();
   }
});
</script>
« Last Edit: May 11, 2022, 11:47:03 AM by mwindey »

*

siken

  • ****
  • 138 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #6 on: May 11, 2022, 05:04:43 PM »
it works for me, thanks

*

siken

  • ****
  • 138 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #7 on: May 11, 2022, 08:48:31 PM »
@siken
Best way to achieve that is to use some kind of jQuery validation, or simply change field type to number.

Found this one:
Code: [Select]
$('input[name="sPhone"]').keyup(function(e)
                                {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});

What is the code for the phone field in the user profile in user-profile.php for <?php UserForm::mobile_text(osc_user()); ?>?

*

siken

  • ****
  • 138 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #8 on: May 11, 2022, 08:50:12 PM »
@siken,

I tested the answer from MB themes and works correct.... only nr no letters allowed.
Maybe you have putted it in the wrong place? This is how it works in veronika
Code: [Select]
         
<div class="row">  <label for="phone"><?php _e('Mobile Phone''veronika'); ?><?php if(strpos($required_fields'phone') !== false) { ?><span class="req">*</span><?php ?></label>
<div class="input-box"><input type="tel" id="sPhone" name="sPhone" value="<?php echo $prepare['s_phone']; ?>" /><i class="fa fa-phone"></i></div>
<script>
 $('input[name="sPhone"]').keyup(function(e)                              {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});
</script>

To allow 1 symbol like i.e: +91 use following:
Code: [Select]
<script>
 $('input[name="sPhone"]').on("input", function(evt) {
   var self = $(this);
   self.val(self.val().replace(/[^0-9\+]/g, ''));
   if ((evt.which != 46 || self.val().indexOf('+') != -1) && (evt.which < 48 || evt.which > 57))
   {
     evt.preventDefault();
   }
});
</script>



What is the code for the phone field in the user profile in user-profile.php for <?php UserForm::mobile_text(osc_user()); ?>?

*

mwindey

  • *****
  • 485 posts
Re: delete spaces and symbols in s_phone field of itempost.php
« Reply #9 on: May 13, 2022, 02:02:32 PM »
@siken
There you go:  ;)
Also land phone is there already before you ask  ;D

Code: [Select]
         
<div class="row">
<label for="phoneMobile"><span><?php _e('Mobile phone''veronika'); ?></span><span class="req">*</span></label>
<div class="input-box" name="s_phone_mobile" ><?php UserForm::mobile_text(osc_user()); ?><i class="fa fa-mobile"></i></div>
<script>
 $('input[name="s_phone_mobile"]').keyup(function(e) {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});
</script>
</div>

<div class="row">
<label for="phoneLand"><span><?php _e('Land Phone''veronika'); ?></span><span class="req">*</span></label>
<div class="input-box" name="s_phone_land" ><?php UserForm::phone_land_text(osc_user()); ?><i class="fa fa-phone"></i></div>
<script>
 $('input[name="s_phone_land"]').keyup(function(e) {
  if (/\D/g.test(this.value))
  {
    // Filter non-digits from input value.
    this.value = this.value.replace(/\D/g, '');
  }
});
</script>
</div>       
« Last Edit: May 13, 2022, 02:15:05 PM by mwindey »