Javascript validation Sentence Case

please give me the code for checking the company name entered in textbox using javascript .Only sentence case to be allowed.Only abbreviations (without a full name) should not be allowed.(eg DPS) Abbreviations if any should be allowed only at the end of the name within ().eg Delhi Public School(DPS)

How can you make the difference between an abbreviation and a short company name?

Eg. “IBM”

That’s what i see difficult

See you :cool:

yes a short company name should only have the first letter in uppercase .here it should be Ibm…iF User enters as IBM then throw an alert box(error)

I see now, then a code verifing only first and second letters in capitals may be like that



function isCompAbbrev(text)
{
    var str = new String(text);
    if (new String(str.substring(0,1)).toUpperCase() == str.substring(0,1)){
         //checks if the first is in caps.
         if (new String(str.substring(1,2)).toUpperCase() == str.substring(1,2)){
                 //two first letters in caps, so abbreviation supposed 
                 return true;
         }
    }
    return false;
}

See you :cool:

No no…Only the first letter should be in uppercase…if its IBM then it can only be entered as Ibm…
Second letter cannot be caps expt unless we are using abrevations towards the end of company name like say…Tech Mahin Corporation(TMC) is valid.

i mentioned about sentence case in the first post…i,e every first letter of a name should be uppercase like instead of TECH MAHIN it should be Tech Mahin…If its only a 1 word name like IBM…it should be Ibm…

You the could use something like this code


function verifCompName(compName)
{
    var str = new String(compName);
    var mtx = new Array();
    mtx = str.split(" ");
    var cl = 0;

    for (cl in mtx){
        var str2 = new String(mtx[cl]);
        if (cl == (mtx.length-1)){
             if (str2.indexOf("(")!=-1 || str2.indexOf(")")!=-1){
                  //supossed Abbreviation, do not checks if it is really in caps
                  return true;
             }
        }
        if (isCompAbbrev(mtx[cl]))
            return false;
    }
    return true;
}

function isCompAbbrev(text)
{
    var str = new String(text);
    if (new String(str.substring(0,1)).toUpperCase() == str.substring(0,1)){
         //checks if the first is in caps.
         if (new String(str.substring(1,2)).toUpperCase() == str.substring(1,2)){
                 //two first letters in caps, so abbreviation supposed
                 return true;
         }
    }
    return false;
}

It returns “true” when everything is ok, “false” when not, alert box is up to you.

See you :cool:

But if you write IBM (International Business Machines) as Ibm then you have written it wrong. It is three capital letters - one for the first letter of each word of its original name.

There are other words that are even more complicated to capitalise correctly such as CoBOL (Common Business Oriented Language) which most people manage to capitalise incorrectly.