Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Form Field Validation Problem with character check

    Hai everyone, I,m just new in javascript but it's getting along nicely. I already wrote my own postal code script which changes in settings with each country that's selected.

    Now I have a new problem which I can't solve atm.

    The name field needs some filtering. I want to build a script which checks each input word in the name field with the following:

    If the second character and/or the fifth character and everything after it are Uppercase it should load the Pcase function which gives the first character of each word uppercase.

    And if there are less then 2 uppercase characters in the entire input the Pcase function should also be activated.

    So what do I basicly want? A validation script which makes sure that the name will be inputted correctly with the Uppercases at the right places.

    I could have automaticly done it which is alot easier but some people have names like MacDonald or McDonald and they wouldn't be displayed correctly.

    I basicly only need to know how I can check each inputted word and check the characters on uppercase, but only nr 2 and 5 and everything after it per word. The rest of the script shouldn't be a problem. the onChar function might work but since i,m quite new I haven't used it yet and my new book won't explain it either.

    I hope you can help me out, and if someone wants my postal code script I'll post it.

    Thanks in Advance

    Dave
    Last edited by Dave122; 03-21-2005 at 10:23 AM.

  • #2
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function checkNaam()
    {
    if (document.forms[0].Regel1.value == ? ) {
    Pcase() }
    return true;
    }

    This is the basic script atm. Regel1 is the form fields name ofcourse.

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have this sofar but it doesn't work at all because whne I type more characters it's over. Any suggestions or other ways to do this?

    function checkNaam()
    {
    rExp = /^([a-zA-Z][A-Z][a-zA-Z][a-zA-Z][A-Z])$/;
    OK = rExp.exec(document.forms[0].Regel1.value);
    if (!OK)
    {
    alert("Wrong Name")
    return false;}
    else {return true;}
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Why do you not simply apply the Pcase function which sets the first character of each word to uppercase, regardless of how it was input.

    If you are worried about MacDonald, McTavish, O'Reilly, D'Ath, B'stard and so on you can test for these less common names specially with regular expressions.

    Split the string after Mac, Mc, O', D' etc., set the next character to uppercase, rejoin string. Same applies to double-barrel names such as Smith-Jones with a hyphen.
    Last edited by Philip M; 03-21-2005 at 01:02 PM.

  • #5
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Though you are right about that, the meaning was that the Pcase would only activate if there were alot of Uppercase characters or alot of lowercase characters (Which probably means less then 2 Uppercase characters) I know it seems a bit stupid and confusing. Then there was this Mac rule which is the main problem.

    Could you show me a example of your explantion? I don't really know how to add it so it checks each seperate word for the uncommon names.

    Thanks in advance, I owe you one
    Last edited by Dave122; 03-21-2005 at 02:18 PM.

  • #6
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It doesn't work right now but could this be in the right direction to check the second character of each word for a Uppercase?

    Code:
    function checkNaam() 
    {
    var text = document.forms[0].Regel1.value;
    var test = /^([A-Z])$/
    splitstring = text.split(" ");
    
    if String.charAt(1) == test {
    alert("Wrong Name")
    return false;
    }
    else {return true;}
    }

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Here is the function which I use in my order form to format the names and addresses. Not every line may be relevant to you. It covers c/o and BFPO addresses, and of course street numbers. You can simplify it quite a bit if you only want to format names.

    The strategy is to convert the whole line to lower case, then identify characters which need to be upper case.



    function changeCase(addrline) {
    var tmpStr = addrline.value.toLowerCase();

    tmpStr = tmpStr.replace (/(\d+)([a-z]{3,})/gi, "$1 $2"); // add space after numbers when 3+ alphachars follow
    tmpStr = tmpStr.replace (/([a-z]{3,})(\d+)/gi, "$1 $2"); // add space after 3+ alphachars when numbers follow
    tmpStr = tmpStr.replace (/\,/g, " "); // delete commas - replace by space (essential!)
    tmpStr = tmpStr.replace (/(\d+)([.{2,}$])/gi, " $1$2"); // add 4 spaces before numbers not at end of string
    tmpStr = tmpStr.replace (/(\S*)\"(\D+)\"(\S*)/g, '$1 "$2" $3'); // put spaces around "string" (force uppercase)
    tmpStr = tmpStr.replace (/o\'/gi, "O' "); // change o' to O'+space (O' Reilly) (force uppercase)
    tmpStr = tmpStr.replace (/(\s|\.)(mc)([a-z])/g, "$1mc $3"); // add space after Mc (force uppercase)
    tmpStr = tmpStr.replace (/(\s|\.)(macc)([a-z])/g, "$1macC$3"); // add space after MacC (force uppercase)
    tmpStr = tmpStr.replace (/(\D)\./g, "$1. "); // add space after literal . (B. M. Smith > uppercase)
    tmpStr = tmpStr.replace (/\-([a-z])/g, " - $1"); // add spaces around hyphens (force uppercase)
    tmpStr = tmpStr.replace (/(^\s+)/, ""); // remove spaces at start of string
    tmpStr = tmpStr.replace (/\s{4,}/g, " "); // remove excessive spaces > 4

    if ((/fpo\b/.test(tmpStr)) || (/fpo\d/.test(tmpStr))) { // FPO and BFPO
    tmpStr = tmpStr.toUpperCase();
    tmpStr = tmpStr.replace (/([A-Z])(\s{2,})/g, "$1 "); // remove excess spaces >1 between A-Z and A-Z/0-9 (not converse)
    }


    var SplitStuff = tmpStr.split(" ");
    var ArrLen = SplitStuff.length;

    for (var k=0; k<ArrLen; k++) {
    var word = SplitStuff[k];
    if (/\d/.test(word)) {word = word.toUpperCase()}
    var wordLen = word.length;
    var posn = word.search(/[a-zA-Z]/);
    var firstChars = word.substring(0, posn+1).toUpperCase();
    var postString = word.substring(posn+1,wordLen);
    word = firstChars + postString;
    word = word.replace ( /C\/O\b/g, "C\/o" );
    word = word.replace ( /Po\b/g, "PO" );
    word = word.replace (/\'a/, "'A"); // De'Ath and D'Arcy

    SplitStuff[k] = word;
    }

    var newline = SplitStuff.join(" ");
    newline = newline.replace (/\.\s+/g, "."); // re-format names B. M. Smith > B.M.Smith
    newline = newline.replace (/\'\s+/g, "'"); // reformat O'Neill etc.
    newline = newline.replace (/Mc\s+/,"Mc"); // reformat McXxxx
    addrline.value = newline;
    }

    By the way, I think Macdonald and McTavish are the correct forms, upper case only after Mc and not after Mac unless MacCxxxx (two c's). This is covered above.
    Last edited by Philip M; 03-21-2005 at 03:29 PM.

  • #8
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works indeed, but I could also work for me with a easier script. One that checks each word for a uppercase at char2 and 5 and everything after that. If it finds one the pcase function should be called. and If there less then 2 characters in the entire input the pcase function should also be called.

    This script you posted works fine and I can easely adjust it but if it checks like above it works easier for me. And the exceptins won't be needed either so it saves alot of bytes I think

    Any way this can be done?

    Right now I have this. Ofcourse in the end it should call the Pcase function instead of a alert but that's for later when it fully works.

    function Test() {

    var fout = /[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/
    var SplitStuff = document.forms[0].Regel1.value.split(" ")
    var ArrLen = SplitStuff.length
    var check = ArrLen.indexOf(fout) > 4 && ArrLen.indexOf(fout) == 1

    if (check)
    { alert("Wrong Name")
    return false;
    }
    return true;
    }

    I,m getting the error though that Arrlen is not a function. Probably easy to fix but I can't find the bug atm. Hope you can help me out.
    Last edited by Dave122; 03-22-2005 at 10:15 AM. Reason: Adding extra code


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •