Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8429461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:20:45+00:00 2026-06-10T05:20:45+00:00

this may be a stupid question or a typo but I’m going to ask

  • 0

this may be a stupid question or a typo but I’m going to ask anyway… I have the following form and when the form is submitted I want to check all the values of the input type="number" and make sure that if the user has for some reason put a zero at the start of the input, for example 01, 025, 0100, to remove the first zero as long as the value isn’t zero. A simple piece of JavaScript made even easier with a jQuery selector. Note that all the text boxes here are type="number" not type="text" and please note my input

enter image description here

Here’s my code:

$('input[type=number]').each(function(){
    // only if the string is not zero
    if(this.value.charAt(0) == "0" && this.value != "0"){
        this.value = this.value.substring(1);
    }   
});

However when I came back to test my form fully I noticed my code didn’t work! So I added the following to write to the console to check what was going on:

console.log("typeof:" + typeof(this.value) + " " + this.id + " char:" + this.value.charAt(0) + " val:" + this.value )

And this gave me the following output, notice the last 4 items that don’t have the value of zero:

enter image description here

Why am I not getting the this.value.charAt(0) when the user has changed the default value of zero?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:20:47+00:00Added an answer on June 10, 2026 at 5:20 am

    It seems that there are spaces in your value. Trim it before comparing (e.g. with jquery, or see here if you don’t want to include jquery just for that):

    $('input[type=number]').each(function(){
        // only if the string is not zero
        var myval = $.trim(this.value);
        this.value = myval.replace(/^[0]+/g,"");
    
    });
    

    The above solution removes arbitrary leading zeroes, that part was taken from this answer to a similar question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a stupid question but I have a code with the following
This may be a stupid question, but I have to ask! I have the
I know this may be a stupid question to ask but I have really
I know this may be a stupid question to ask but I want to
This may be a stupid question to ask, but still I need to know
I'm not a DBA so this may be a stupid question but I'll ask
This may be a stupid question, but I'll feel stupider if I don't ask
This may be a very stupid question, but as I'll try it anyway. I'm
This may be a stupid question, but when I write the following in my
This may be a stupid question, but I still have it . Does having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.