SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using charAt to only allow 0-9 or a decimal ...im almost there?

    Hi all,

    I can use the code below and swap the 0 for another number or even a decimal, but how can i enter all of them into the code below? i want to check for numbers 0-9 and a .
    Ive tried some regex type lines in there but still cant get it.
    Do i need to create an array or something?

    i thought it would be something simple, kinda like ...if( value.charAt(i) != (0-9), ".")

    any help would be cool, i assume im on the right track with charAt?

    Code:
    for (i = 0; i < value.length; i++)
        {
           if( value.charAt(i) != "0" )
            {
              alert( "Please Enter a Number" );
            }
        }

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok ive acheived it like this for now

    Code:
    for (i = 0; i < value.length; i++)
        {
           if((value.charAt(i) != 0) 
           && (value.charAt(i) != 1) 
           && (value.charAt(i) != 2)
           && (value.charAt(i) != 3)
           && (value.charAt(i) != 4)
           && (value.charAt(i) != 5)
           && (value.charAt(i) != 6)
           && (value.charAt(i) != 7)
           && (value.charAt(i) != 8)
           && (value.charAt(i) != 9)
           && (value.charAt(i) != "."))
            {
              alert( "Please Enter a Number" );
            }
        }
    id appreciate it if someone can help me minimise this?

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The simplest way to do this is to use a regular expression. Are you sure you don't want to allow decimals?

    Code:
    var notNumRE = new RegExp('\\D'); 
    if( notNumRE.test(value) )
    {
      alert( "Please Enter a Number" );
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Kravvitz,

    Thanks for the reply.

    i need to allow only 0-9 and 1 decimal.
    the reason is because its a value as in money. so it really needs the decimal there too.

    What ive done works, but i would much prefer to use some code like you just specified there. I like to keep things tidy and effecient as possible

    the other way ive been looking at is put 0-9 and . in a string and search for each character in the string...but im not sure if thats as good?

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code:
    var moneyRE = new RegExp('^\\d(\.\\d{2})?$'); 
    if( !moneyRE.test(value) )
    {
      alert( "Please Enter a Number" );
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regexps will work fine, but the typeof construct can be used here:
    Code:
    var object = 53523.5;
    if (typeof object != "number") {
    	if (object != ".") {
    		alert("must be a number");
    		}
    	}
    Mike - Side project: Live Journal (blogging site).

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mburt, you need to cast the value to a Number first if you want to use your solution. Also, that solution will not handle the scenario when the user only types in a dot. It also doesn't take into account that only one decimal is allowed.

  8. #8
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My point still stands
    Code:
    var object = 53523.5;
    if (typeof object != "number") {
    	if (!object.indexOf(".")) {
    		alert("must be a number");
    		}
    	}
    Mike - Side project: Live Journal (blogging site).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •