SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: form check

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form check

    I want to do a check where a field has to be an integer and the length has to be 4 or 6. Can someone help?

    I'm trying to do the length thing but it is not working and I have no idea how to do the integer check.

    if (document.comment.cust.value.length != 6 || 4)
    {
    alert("Please enter a valid Cust #")
    document.comment.cust.focus()
    return false
    }

    I can do this so if less than 4, but I really don't want 5,7,8,9... to go through:

    if (document.comment.cust.value.length < 4)
    {
    alert("Please enter a valid Cust #")
    document.comment.cust.focus()
    return false
    }

    Thanks.
    Last edited by jamesb; Apr 10, 2002 at 09:51.

  2. #2
    SitePoint Zealot Drew630's Avatar
    Join Date
    Nov 2001
    Location
    Maryland
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (document.comment.cust.value.length > 6 || document.comment.cust.value.length < 4 || document.comment.cust.value.length == 5) 
    { 
    alert("Please enter a valid Cust #") 
    document.comment.cust.focus() 
    return false 
    }
    that should take care of the length. NOw, as far as being a number, variables are variant by default, meaning they have no set type. Therefore, there is no easy way to check if it is an integer. The best way that I can think of is to parse the variable value witha for loop and test each character to see if it is a number. Got that?

    Drew
    ~Drew

    There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The best way to do this is with a regular expression:

    http://www.webreference.com/js/column5/

    Unfortunately, I am very bad at them and won't lead you astray with my pitiful attempt to construct one. Some others on this forum can probably post some code for you.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. The length thing worked great. I found this code to check for integer:

    <script language="Javascript">

    function showData() {
    var oldstring = document.formone.inputone.value;
    var newstring = parseFloat(oldstring).toString();
    var InpValid=1;
    if (oldstring.length == newstring.length &amp;&amp; newstring != "NaN") {
    alert("Input is a number");
    }
    else {
    alert("Input is not a number");
    InpValid=0;
    }
    }

    </script>

    I won't alert if it is OK, but is this kinda what you were refering to? Hopefully I can get it working.

    Thanks.

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or this checks each character

    PHP Code:

    function isInt(textObj)
    {
        var 
    newValue textObj.value;
        var 
    newLength newValue.length;

        for(var 
    0!= newLengthi++)
        {
            
    aChar newValue.substring(i,i+1);
        
            if(
    aChar "0" || aChar "9")
            {
                return 
    false;
            }
        }
        return 
    true;

    this makes sure your value does not have an integer

    then just use an if statement to check if the integer is between the amount you want

  6. #6
    SitePoint Zealot Drew630's Avatar
    Join Date
    Nov 2001
    Location
    Maryland
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    James -
    That code is a lttle weird, but it looks like it will work. Post if you can't get it to work.

    AndrewJ -
    While your function will work great, it isn't ideal for this situation, since PHP is server-side and form checking is ideal in Javascript, so unnecessary calls to the server aren't made.

    Drew
    ~Drew

    There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.

  7. #7
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason I posted that code was because I thought it would save time on the users side.

    What I mean is that its extremely annoying to post a msg to find out you've made a little mistake. With javascript checking on the clientside it makes this much easier to deal with without waiting for that error to come back from php, perl, or asp.

    Even if javascript is disabled, on the server side it would be ideal to have the code checked this is purely to do with usability.

    Even if you want the page to be rendered differently you could always use the dom to make changes. However you can always influence where you want the page to go by using document.form1.action="my_target_page.php" so its more clientside than serverside.

    any way hope that helped


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
  •