Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
country code will automatically added before mobile number

If any one type in input box only number like : 01234567890 (total 13 digit with country code) then automatically add 88 before mobile number but if if anyone number with 8801234567890 it wont added before number. Another one if type 1234567890 total 10 digit then add 880 before number. how to fix it? I tried with add value but its not working. I need only my conditions not every time.

$(document).ready(function() {

    $('#phone').keyup(function() {
        let total_length = this.value.length;
        
        if(total_length='11'){
          $("#phone").val("88"+$("#phone").val());
        }
        else if(total_length='10'){
          $("#phone").val("880"+$("#phone").val());
        }
        else{
           $("#phone").val();
        }
    });

    $.validator.addMethod("countryValid", function(value, element) {
        return this.optional(element) || /^(?:\+88|88)?(01[3-9]\d{8})$/i.test(value);
      }, "Please enter valid phone no."); 

  
    $("#my_form").validate({
        rules: {
        phone : {
            required: true,
            number: true,
            countryValid: true
        }
        }
    });

  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>

<form id="my_form">

<input type="text" name="phone" id="phone" />
<br><br>
<button type="submit">Submit</button>

</form>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Fixed it with replace keyup to change and add the ==(Equal to) from =(assignment operators)

$('#phone').change(function() {
        let total_length = this.value.length;
        
        if(total_length=='11'){
          $("#phone").val("88"+$("#phone").val());
        }
        else if(total_length=='10'){
          $("#phone").val("880"+$("#phone").val());
        }
        else{
           $("#phone").val();
        }
    });

Thanks @CherryDT for this help.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!