Identifying invalid char position using RegExp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Brennan

    Identifying invalid char position using RegExp

    Good morning.

    I would like to use one or more RegExps to validate country names as having
    the first and last words beginning with an uppercase letter, intermediate
    words beginning with either uppercase or lowercase, and all other characters
    being lowercase characters. For example:

    Turks and Caicos

    The difficult part is that I want to obtain save the string positions of the
    invalid characters in an array, rather than just obtain a true or false
    value.

    Can anybody with more experience with Regular Expressions than me suggest
    some code, sites, or approaches.

    Thanks,
    Ron


  • Evertjan.

    #2
    Re: Identifying invalid char position using RegExp

    Ron Brennan wrote on 20 nov 2004 in comp.lang.javas cript:[color=blue]
    > Good morning.[/color]

    Not for us overhee in Europe ;-)

    [color=blue]
    > I would like to use one or more RegExps to validate country names as
    > having the first and last words beginning with an uppercase letter,
    > intermediate words beginning with either uppercase or lowercase, and
    > all other characters being lowercase characters. For example:
    >
    > Turks and Caicos
    >
    > The difficult part is that I want to obtain save the string positions
    > of the invalid characters in an array, rather than just obtain a true
    > or false value.[/color]

    simple testing is a Regex job:

    s = 'Turks and Caicos'
    alert(/^[A-Z][a-z]+ ([A-Za-z][a-z]+ )*[A-Z][a-z]+$/.test(s))

    however your question is more a looping afair, not simple regex:

    =============== ========

    yourtest('Turks and Caicos')
    yourtest('turKs anD cCaicos')
    yourtest('turks And cCaicoS')

    function yourtest(s){
    stest = ' '
    for (i=0;i<s.length ;i++){
    LastIsSpace = / /.test(stest)
    stest = s.substr(i,1)
    if (
    i==0 &&
    /[^A-Z]/.test(stest)
    ) // test first letter
    document.write( i+' ff '+stest+'<br>')
    else if (
    LastIsSpace &&
    /[^A-Z ]/.test(stest) &&
    !/ /.test(s.substr( i,999))
    ) // test first letter last word
    document.write( i+' ff '+stest+'<br>')
    else if (
    !LastIsSpace &&
    /[^a-z ]/.test(stest)
    ) // test nonfirst letters
    document.write( i+' FF '+stest+'<br>')
    else // OK
    document.write( i+' OK '+stest+'<br>')
    }
    document.write( '<br><br>')
    }

    =============== ========

    Please make your own array.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)

    Comment

    • Ron Brennan

      #3
      Re: Identifying invalid char position using RegExp

      Yes, that appears to be exacly what I wanted. I'll confirm that it works in
      my application when I finish.

      Thanks to Evertjan for the generous contribution of time and effort.

      Ron



      Comment

      • Dr John Stockton

        #4
        Re: Identifying invalid char position using RegExp

        JRS: In article <ROOdnSKT6YGF_Q LcRVn-hw@magma.ca>, dated Sat, 20 Nov
        2004 10:16:27, seen in news:comp.lang. javascript, Ron Brennan
        <rbrennan@magma .ca> posted :[color=blue]
        >
        >I would like to use one or more RegExps to validate country names as having
        >the first and last words beginning with an uppercase letter, intermediate
        >words beginning with either uppercase or lowercase, and all other characters
        >being lowercase characters. For example:
        >
        >Turks and Caicos[/color]

        That policy fails for, among others,
        British Virgin Islands
        Campione d'Italia
        Saint-Pierre et Miquelon
        Turks and Caicos Islands
        British Antarctic Territory
        British Indian Territory
        French Southern Territories
        Juan de Nova Island
        South Georgia and the South Sandwich Islands
        Peñón de Vélez de la Gomera
        and, of course,
        the United States of America
        Stato della Citta del Vaticano
        the United Kingdom of Great Britain and Northern Ireland
        Poblacht na h'Eireann
        La Republique de la Cote d'Ivoire
        Daulat Libiya al-'Arabiya al-Ishtrakiya al-Jumhuriya
        Zhonghua Renmin Gongheguo
        Chung-hua Min Kuo
        and others.

        Including Lord Howe Island; a place of interest that Win98 seems not to
        be configurable for.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
        Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
        PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
        Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

        Comment

        • Ron Brennan

          #5
          Re: Identifying invalid char position using RegExp

          [color=blue]
          > That policy fails for, among others,
          > British Virgin Islands
          > Campione d'Italia
          > Saint-Pierre et Miquelon
          > Turks and Caicos Islands
          > British Antarctic Territory
          > British Indian Territory
          > French Southern Territories
          > Juan de Nova Island
          > South Georgia and the South Sandwich Islands
          > Peñón de Vélez de la Gomera
          > and, of course,
          > the United States of America
          > Stato della Citta del Vaticano
          > the United Kingdom of Great Britain and Northern Ireland
          > Poblacht na h'Eireann
          > La Republique de la Cote d'Ivoire
          > Daulat Libiya al-'Arabiya al-Ishtrakiya al-Jumhuriya
          > Zhonghua Renmin Gongheguo
          > Chung-hua Min Kuo
          > and others.
          >
          > Including Lord Howe Island; a place of interest that Win98 seems not to
          > be configurable for.
          >
          > --
          > © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00[/color]
          MIME. ©[color=blue]
          > Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links,[/color]
          acronyms[color=blue]
          > PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see[/color]
          00index.htm[color=blue]
          > Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm[/color]
          etc.

          The policy was a prototype I tentatively adoped when I didn't immediately
          think of any of the many cases you cited. Obviously the policy has to be
          rewritten and probably requires as part of it an enumeration of exceptions.
          I'll see what I can come up with tomorrow and post it here for review.



          Comment

          • Rob B

            #6
            Re: Identifying invalid char position using RegExp



            Have you considered

            1) doing this at the server
            2) using 'dependent' listboxes at the client

            Both would present the user with valid choices, rather than attempting
            an algorithm to validate such a wide array of possibilities.. ..

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...