User-friendly data-validation is important. Rarely is a simple, red asterisk next to a form field enough to indicate what’s wrong with the input. Is it a required field? Is it too high? Is it not allowed? Generally, it’s a good idea to indicate what’s wrong with the form and how to fix it. For example, a good message for a poorly formatted currency field might be “must be formatted like currency (e.g. $9999.99).”

On the other hand, it can be easy to go too far. Take this snippet of validation code that Dai uncovered, for example. Even the most obtuse of users don’t need to be told “You must have a minus sign in the 1st position or immediately after a $ sign.”

function validInt(passint)
{
    var okstring = "^0^1^2^3^4^5^6^7^8^9^.^,^-^$^%^"
    var str = 	passint
       
    re = /; ]/g;
    r = str.replace(re,"")
    str=r
           
    var arraystr = str.split("")
    var alength =arraystr.length
    for (i=0; i < alength; i++)
    {
    srchchar = "^"+arraystr[i]+"^"
    findchar= okstring.indexOf(srchchar)
        if (findchar == -1)
        {
        return("Field contains non numeric data ")
        }
    }

    fdot = str.indexOf(".")
    ldot = str.lastIndexOf(".")
    if (fdot != ldot)
    {
        return("You have more than one decimal")           
    }          

    if (fdot > -1 && str.length==1 )
    {
        return("You only have a decimal point(.) in this field")           
    }      
       

       
    fdollar = str.indexOf("$")
    ldollar = str.lastIndexOf("$")
    if (fdollar != ldollar)
    {
        return("You have more than one dollar sign")       

    }
       
    if (fdollar > -1 && str.length==1 )
    {
        return("You only have the $ sign in this field")           
    }      
       

    fminus = str.indexOf("-")
    lminus = str.lastIndexOf("-")
    if (fminus != lminus)
    {
        return("You have more than one minus sign")        
    }      
       
    if (fminus > -1 && str.length==1 )
    {
        return("You only have the minus sign in this field")       
    }      
       

    if (fdollar > -1 && fminus > -1)
    {
    //return("You cannot use both a dollar($) sign and a minus(-) sign")       
    }      
       
       
    if (fdollar > 0)
    {
        return("You must have a dollar sign in the 1st position")          
    }      
       
    if (fminus > 0)
    {
        if (fdollar == 0 && fminus == 1)
        {
            if (str.length==2)
            {
            return("You must have a value after a $- sign")
            }
        }
        else
        {
        return("You must have a minus sign in the 1st position or immediately after a $ sign")     
        }
    }          
       
    fpercent = str.indexOf("%")
    lpercent = str.lastIndexOf("%")
    if (fpercent != lpercent)
    {
        return("You have more than one percent sign")      
    }      
       
    if (fpercent > -1 && fpercent != str.length-1 )
    {
        return("You must put the percent sign in the last position ")      
    }      
       
    if (fpercent > -1 && str.length==1 )
    {
        return("You only have the % sign in this field")           
    }      


       
    return("")
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!