SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    validate that numbers are entered

    Im back. Im trying to make sure that when a user enters information into my textbox, that when the textbox loses focus then it checks to make sure that they entered numbers and not text or other characters. Numbers only....Any suggestions??

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using regular expressions for example this is a function on use to call when a field is off focus. The format of the number must be ##.## or #.## etc...

    When the number is entered in the function the number is checked via regular expression change the \d\d to whatever you like in this format

    /\d\d\.\d\d/ That would check for a any numeric value that is ##.## only start the expression with / and end it with / so if you want a 4 digit number only (no less no more) /\d\d\d\d/

    Anyways that gives you a start it can be far more complicated than that.

    Also if you want multiple choices just place a | in between them
    /\d\d\.\d\d|\d\d\d\d/


    function numericTest(value){
    var valuein=new String(value)
    var regExpObj=/\d\d\.\d\d|\d\.\d\d|\d\d\.\d|\d\d|\d/;
    if (valuein.indexOf(".") == -1 && valuein.length >3){
    alert('you have not input a valid number - the format must be in ##.## or ###.##')
    return false
    }
    if (regExpObj.exec(valuein) == null || valuein < "1" || valuein.indexOf(".")>3)
    {
    alert('You have not inputed a numberic value - the format must be ##.## or ###.##')
    return false
    }
    return true
    }


    Hope that helps
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I actually got this to work a different way. This checks to make sure the user enters in a number 4 digits long, no more and no less. It works great!

    function check(field3)


    {
    var f = field3.value;
    var tempNum = Math.floor(f / 10000);
    var tempNum1 = Math.floor(tempNum / 1000);
    var tempNum2 = Math.floor(tempNum1 / 100);
    var tempNum3 = Math.floor(tempNum2 / 10);
    if ((f.length < 4) || (f.length > 4) || (tempNum3 != 0))
    {
    alert("Not a valid Numer");
    field3.value = ""
    field3.select();
    field3.focus()

    return false;
    }
    }

    Then on the textfield I did this...

    <input type="text" name="mos" size="8" tabindex="5" ONCHANGE="check(form.mos)">

    So when the user enters a wrong value and clicks out of the textbox it gives the alert, then clears the field and makes them type it in again....

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Brad,

    You might find the following interesting:

    <form name="a">
    <input type="text" name="b" onKeyup="chk(this.value)">
    </form>

    <script>
    function chk(fldVal)
    {
    if(fldVal.length > 4)
    {
    document.a.b.value = document.a.b.value.substr(0,4);
    document.a.b.select();
    }
    if(isNaN(fldVal))
    document.a.b.select()
    }
    </script>

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


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
  •