SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2000
    Location
    UK
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to know if anyone has a simple HTML code for validating input "Text" values so that form will either not accept any non integer, non positive, non numeric value. I am open to suggestions for look-up tables but as the numeric quantity value could be 1-1000 it would make them unwieldy, similarly knocking back the order form (which could have multiple line orders) because one of the values does not fit works but is tiresome. Would prefer answer to be JAVA driven as I am building a WEB hosted E Shop which will work on and off the WEB and just use the WEB to submit/authenticate orders.

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    HTML can not validate any value entered into a form. You would have to use some sort of scripting engine like javascript.

    Anyways here is some code that you can look at modifying for your purposes.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    // isInteger (STRING s [, BOOLEAN emptyOK])
    //
    // Returns true if all characters in string s are numbers.
    //
    // Accepts non-signed integers only. Does not accept floating
    // point, exponential notation, etc.
    //
    // We don't use parseInt because that would accept a string
    // with trailing non-numeric characters.
    //
    // By default, returns defaultEmptyOK if s is empty.
    // There is an optional second argument called emptyOK.
    // emptyOK is used to override for a single function call
    // the default behavior which is specified globally by
    // defaultEmptyOK.
    // If emptyOK is false (or any value other than true),
    // the function will return false if s is empty.
    // If emptyOK is true, the function will return true if s is empty.
    //
    // EXAMPLE FUNCTION CALL: RESULT:
    // isInteger ("5") true
    // isInteger ("") defaultEmptyOK
    // isInteger ("-5") false
    // isInteger ("", true) true
    // isInteger ("", false) false
    // isInteger ("5", false) true

    function isInteger (s)

    { var i;

    if (isEmpty(s))
    if (isInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i &lt; s.length; i++)
    {
    // Check that current character is number.
    var c = s.charAt(i);

    if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
    }
    // isSignedInteger (STRING s [, BOOLEAN emptyOK])
    //
    // Returns true if all characters are numbers;
    // first character is allowed to be + or - as well.
    //
    // Does not accept floating point, exponential notation, etc.
    //
    // We don't use parseInt because that would accept a string
    // with trailing non-numeric characters.
    //
    // For explanation of optional argument emptyOK,
    // see comments of function isInteger.
    //
    // EXAMPLE FUNCTION CALL: RESULT:
    // isSignedInteger ("5") true
    // isSignedInteger ("") defaultEmptyOK
    // isSignedInteger ("-5") true
    // isSignedInteger ("+5") true
    // isSignedInteger ("", false) false
    // isSignedInteger ("", true) true

    function isSignedInteger (s)

    { if (isEmpty(s))
    if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
    else return (isSignedInteger.arguments[1] == true);

    else {
    var startPos = 0;
    var secondArg = defaultEmptyOK;

    if (isSignedInteger.arguments.length &gt; 1)
    secondArg = isSignedInteger.arguments[1];

    // skip leading + or -
    if ( (s.charAt(0) == "-") &#0124; &#0124; (s.charAt(0) == "+") )
    startPos = 1;
    return (isInteger(s.substring(startPos, s.length), secondArg))
    }
    }


    // isPositiveInteger (STRING s [, BOOLEAN emptyOK])
    //
    // Returns true if string s is an integer &gt; 0.
    //
    // For explanation of optional argument emptyOK,
    // see comments of function isInteger.

    function isPositiveInteger (s)
    { var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length &gt; 1)
    secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine. What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    // i) s is empty and we are supposed to return true for
    // empty strings
    // ii) this is a positive, not negative, number

    return (isSignedInteger(s, secondArg)
    && ( (isEmpty(s) && secondArg) &#0124; &#0124; (parseInt (s) &gt; 0) ) );
    }


    // isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
    //
    // Returns true if string s is an integer &gt;= 0.
    //
    // For explanation of optional argument emptyOK,
    // see comments of function isInteger.

    function isNonnegativeInteger (s)
    { var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length &gt; 1)
    secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine. What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    // i) s is empty and we are supposed to return true for
    // empty strings
    // ii) this is a number &gt;= 0

    return (isSignedInteger(s, secondArg)
    && ( (isEmpty(s) && secondArg) &#0124; &#0124; (parseInt (s) &gt;= 0) ) );
    }
    [/code]

    I think that covers everything you were asking except the less than 1000 part but that could be as simple as:
    if s &gt; 1000 return false;


    I hope this helps




    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com


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
  •