[thelist] [JS] regex function vs. plain function

Tom Dell'Aringa pixelmech at yahoo.com
Fri Apr 9 09:29:08 CDT 2004

Hi folks,

We're using a function that checks for a valid number entry. It works
fine but I am wondering if a regex would be more efficient - it
certainly seems it would be smaller. Here is the function:

// Check for valid number
function isNumber(fieldname, message, fieldLength, setFocus) {
    if (fieldname == null) return "";

    var digits = "0123456789";   
    var field = fieldname.value;
    if (field.length==0) return ""; 
    if (fieldLength > 0) {
      if (field.length != fieldLength) {
        if (setFocus) fieldname.focus();
        return message;

    for (var i=0; i < field.length; i++) {
        temp = field.substring(i, i+1);
        if ((digits.indexOf(temp) == "-1")) {
            if (setFocus) fieldname.focus();
            return message;
    return "";

Wouldn't this be better as a regex? We have like 3 or 4 of these...


http://www.Pixelmech.com/ - read my latest blog posting!
http://www.DMXzone.com/ - JavaScript Author
http://SparklesParties.com - Princess parties for little girls!
http://www.thywordistruth.net/ - Eternal Life

"Well, my name's Dewey Oxburger. My friends call me Ox. I dont know if you've noticed, but I got a slight weight problem."

More information about the thelist mailing list