SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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)

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Location
    Bs. As. - Argentina
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can you make the difference between an abbreviation and a short company name?

    Eg. "IBM"

    That's what i see difficult


    See you

  3. #3
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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)

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Location
    Bs. As. - Argentina
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see now, then a code verifing only first and second letters in capitals may be like that

    Code JavaScript:
     
    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

  5. #5
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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..

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Location
    Bs. As. - Argentina
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You the could use something like this code

    Code JavaScript:
    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

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by maria_megha View Post
    If its only a 1 word name like IBM..it should be Ibm..
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

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
  •