Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JavaScript Error Checking - Don't Want Strings

Options
  • 11-04-2004 7:01pm
    #1
    Registered Users Posts: 1,278 ✭✭✭


    i'm trying to improve the error checking on my physics project, which can be seen at:
    http://www.thegalwaysky.com/starcheck/

    currently if the user chooses "Custom Location" from the drop-down, and enters a string in one of the textboxes (as opposed to what should be a number), my error checking does not pick up on it.

    is there some way of checking whether a form field value is a string or a number ?

    eg. if (document.myform.myfield.value == string)
    or something to that effect....


Comments

  • Registered Users Posts: 1,746 ✭✭✭pork99


    just off the top of my head isn't it one of the features of Javascript that it is a loosely typed language?

    maybe use form validation with Regular Expressions?

    something like

    var regexp = [\d]

    will search a string for only digits 0-9

    Not too well up on this sort of thing, I've only used it before to cobble together form validation to check for valid emails and the like.

    Also theres the isNaN() function, I've never used it just dimly aware of it :)


  • Closed Accounts Posts: 304 ✭✭Zaltais


    Yep - you could use isNan(), but personally I'd go for a Regular Expression. Read this

    It's a bit long, but a good reference.


  • Registered Users Posts: 1,278 ✭✭✭jArgHA


    thanx for the tips,

    i think i can figure it out with the help of those links.

    cheers,
    jAH


  • Closed Accounts Posts: 13 ter


    Basically what you need to do here is learn more about string manipulation.
    what you are looking for is

    let us say we have the variable x

    so does x contain the number 0 or 1 or 2 and so on to nine or more importantly does it start with a number . if it does then its a number else its a string.

    off the top my head i would start along those lines .


  • Registered Users Posts: 394 ✭✭Mickah


    function isDigit (c)
    {
    return ((c >= "0") && (c <= "9"))
    }

    // Check whether string s is empty.

    function isEmpty(s)
    {
    return ((s == null) || (s.length == 0))
    }
    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 < 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;
    }

    Your welcome


  • Advertisement
  • Closed Accounts Posts: 304 ✭✭Zaltais


    Guys, you're making it far more complicated than it needs to be.

    It's not a 'string manipulation' or a check of every character in a passed variable to see if its any character between '0' and '9', it's called a 'regular expression'- or RegExp, or RegEx.

    Below is an example of a complete validation - not just the 'digit matching' - that's just handled by the 'isNumber' function - which you'll notice is obscenely short.

    [PHP]

    <script type="text/javascript">
    <!--
    function isNumber(value){
    var re = new RegExp("^[0-9]+$");
    return re.test(value);
    }

    function validate(what){
    if (isNumber(what)){
    window.alert(what + " is a valid number")
    } else {
    window.alert(what + " is NOT a valid number")
    }
    }
    //-->
    </script>

    <form name="form1">
    <input type="text" value="text" name="inputField">
    <input type="button" onClick="validate(document.form1.inputField.value)">
    </form>
    [/PHP]


    The key part of the code above is the "isNumber()" function. This is what does the work.

    Basically to explain this bit of gobbldygook:
    var re = new RegExp("^[0-9]+$");

    [PHP]
    var re // This creates a new varible called 're'
    new Regexp // defines that the 're' variable is a Regexp object
    "^[0-9]+$" // this defines the matching criteria
    " // The quote marks mark the outer boundries of the match criteria
    ^ // matches the begining of a line
    [0-9] // defines a range of characters (in this case anything from '0' to '9')
    + // defines that there should be one or more of the preceeding
    // characters (i.e. anything from '0' to '9') for the match to be true
    $ // defines the end of the line
    [/PHP]

    So your regexp says in laymans terms:
    return a match if the string has a begining of line and then one or more of any character between '0' and '9' and then an end of line.

    You can also change the '+' so that you can match a required number of characters (e.g. 5 digits long exactly, or anywhere between 1 and 10 digits etc)

    It is a bit of a head bender, but it is the 'proper' way to do it. You could just use the example I've given, but if you read the article you'll have a better understanding of what's happening. You might not need it now, but if you understand it's potential applications you WILL use it again.

    Yes I know it's not valid HTML. And no it's not extensively tested. And yes there probably is an even shorter way to write that RegEx, but that one works straight off.


  • Registered Users Posts: 1,278 ✭✭✭jArgHA


    thanx again to all, especially to zaltais

    i founf that the simple isNaN() function served the requirements

    eg.
    if (isNaN(document.myform.myfield.value))
    alert("Please enter a value between 0 and 60!");

    the regular expression stuff is interesting tho, will be using that in future.

    cheers,
    jAH


Advertisement