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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Validation: Only Allow 1 Letter In Form If 2 Words Added

    Hi, i know i should have 2 fields for a first and last name in my form but i dont - and was wondering if someone would be able to help me with - or has - a java script valadation rule to only allow a one letter character (letter in between spaces such as a middle initial) in a text field if at least 2 words are also included (2 words of at least 2 or more characters)

    so if the user input ' John H ' an error prompt would display until one more word is added/at least 2 words are found

    right now im using the below coding to allow at least 2 words in the text field but would like to see if its possible to enhance it so it will only block a one letter word if there are less than 2 other words in the form - right now if any words is added with less than 2 characters it will display an error... if anyone knows of a better form of code that needs a minimum of 2 words added and still allows single characters such as middle initials to in a validation script i'd greatly appreciate your help... i get lost on validation process'

    'cheers' in advance to anyone that may know of something or a site that can point me in a better direction

    function(value,element){return this.optional(element)||/^((\b[a-zA-Z]{2,80}\b)\s*){2,}/

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Lightbulb Something to try ...

    See if this works for you...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?p=1113341#post1113341
    
    function middleInitial() {
      var tmp = document.getElementById('Fullname').value.split(' ');
      if (tmp.length < 3) { return 'Missing FULL name'; }
      var minit = tmp[1].charAt(0);
      return minit;
    }
    </script>
    
    </head>
    <body>
    Full name 
    <br><input id="Fullname" id="text" value="">
    <br>Ex: John Q. Student
    <br><button onclick="alert(middleInitial())">Check initial</button>
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks jmrker, but that makes the middle initial required as well

    thanks for the coding help jmrker, but that seems to make the middle initial required as well...

    the basic problem i'm having is i need a form field to allow a minumum of 2 words... i found a script that kind-of works, but it works by making sure there are at least 2 words in the text field with at least 2 characters in each word, but it looks at the first 2 words to make sure they are more than 2 characters (which screws up allowing a middle initial)

    i guess i need a script that looks to see if at least 2 words were entered with 2 or more characters in each word, but looks at all fields in the form and not just the first 2. im not very good with validation scripts so im not sure what to do

    thanks again for your help jmrker... hopefully i can figure this out so it can allow a middle initial as an option but still require at least 2 words with 2 characters or more in each word

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    881
    Thanks
    0
    Thanked 115 Times in 114 Posts
    Code:
    var temp=TheName.split(' ');
    var Valid=1;
    if( temp.length>1){
    
    for x=0;x<temp.length;x++){
    if (x==0 || x==temp.length-1){   //if first or last word
          if (temp[x].length<2){Valid=0}
    }else{
          if (temp[x].length!=1){Valid=0}
    }
    }
    
       if (Valid==1){
       alert('Thanks');
       }else{
       alert('not a valid name');
       }
    
    }else{
    alert('name to short');
    }
    Last edited by Lerura; 07-19-2011 at 03:02 AM.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Exclamation

    Quote Originally Posted by clhmedia View Post
    Hi, i know i should have 2 fields for a first and last name in my form but i dont - and was wondering if someone would be able to help me with - or has - a java script valadation rule to only allow a one letter character (letter in between spaces such as a middle initial) in a text field if at least 2 words are also included (2 words of at least 2 or more characters)

    so if the user input ' John H ' an error prompt would display until one more word is added/at least 2 words are found

    right now im using the below coding to allow at least 2 words in the text field but would like to see if its possible to enhance it so it will only block a one letter word if there are less than 2 other words in the form - right now if any words is added with less than 2 characters it will display an error... if anyone knows of a better form of code that needs a minimum of 2 words added and still allows single characters such as middle initials to in a validation script i'd greatly appreciate your help... i get lost on validation process'

    'cheers' in advance to anyone that may know of something or a site that can point me in a better direction

    function(value,element){return this.optional(element)||/^((\b[a-zA-Z]{2,80}\b)\s*){2,}/
    Quote Originally Posted by clhmedia View Post
    thanks for the coding help jmrker, but that seems to make the middle initial required as well...

    the basic problem i'm having is i need a form field to allow a minumum of 2 words... i found a script that kind-of works, but it works by making sure there are at least 2 words in the text field with at least 2 characters in each word, but it looks at the first 2 words to make sure they are more than 2 characters (which screws up allowing a middle initial)

    i guess i need a script that looks to see if at least 2 words were entered with 2 or more characters in each word, but looks at all fields in the form and not just the first 2. im not very good with validation scripts so im not sure what to do

    thanks again for your help jmrker... hopefully i can figure this out so it can allow a middle initial as an option but still require at least 2 words with 2 characters or more in each word
    The code is based upon my understanding of your problem.
    Give more examples beyond "John H" and what you expect to see when entered.
    In short, what are the FULL validation rules you wish to implement, not just one

  • #6
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry for any confusion, to be more specific in what im looking for - i have a form field for full names 'fullname', i wanted to make the form have a validation script to require a minimum of 2 words in the field, i found a script that does the job

    [script]

    minvalue:"Please enter your full name."
    minvalue:{minvalue:true}
    minvalue:function(value,element){return this.optional(element)||/^\w{2,}\s+\w{2,}/.test(value);}

    [end script]

    it allows a minimum of 2 words with at least 2 characters per word... but it seems like it only looks at the first 2 words to see if they have at least 2 letters, which creates a problem because if i add my middle initial in the field (John H Smith) it tells me 'Please enter your full name.' even though at least 2 words with at least 2 letters were added...

    what i am looking for is a script to use in a text field that will requires a minimum of 2 words to be entered, with each of the 2 words having at least 2 characters... but i dont want it to only examine the first 2 words in the field, i want it to look at all words entered and make sure there are at least 2 words with 2 or more characters. so entries like 'John Smith', or 'John H Smith' will be allowed whereas enteries like 'J J', 'Joe J', or 'Joe' will produce an error message like Please enter your full name.
    Last edited by clhmedia; 07-19-2011 at 03:33 PM. Reason: put diff script: updated

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Question

    Quote Originally Posted by clhmedia View Post
    ...
    it allows a minimum of 2 words with at least 2 characters per word... but it seems like it only looks at the first 2 words to see if they have at least 2 letters, which creates a problem because if i add my middle initial in the field (John H Smith) it tells me 'Please enter your full name.' even though at least 2 words with at least 2 letters were added...

    what i am looking for is a script to use in a text field that will requires a minimum of 2 words to be entered, with each of the 2 words having at least 2 characters... but i dont want it to only examine the first 2 words in the field, i want it to look at all words entered and make sure there are at least 2 words with 2 or more characters. so entries like 'John Smith', or 'John H Smith' will be allowed whereas enteries like 'J J', 'Joe J', or 'Joe' will produce an error message like Please enter your full name.
    Why would "John H Smith" be legal when the 1st two words (John H) do not satisfy the criteria of at least two characters?
    Also, would "John H. Smith" satisfy your criteria even if the '.' is not a letter?

  • #8
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts
    See if this works any better for you ...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?p=1113341#post1113341
    
    function FullNameCheck() {
      var errMsg = [];
      var tmp = document.getElementById('Fullname').value.split(' ');
      if (tmp.length < 2) { errMsg.push('Missing FULL name'); }
      for (var i=0; i<tmp.length; i++) {
        if (tmp[i].length < 2) { errMsg.push('Word: '+(i+1)+' is too short'); }
      }
      return errMsg.join('\n');
    }
    </script>
    
    </head>
    <body>
    Full name 
    <br><input id="Fullname" id="text" value="John H. Smith">
    <br>Ex: John Q. Student
    <br><button onclick="var tmp=FullNameCheck();if (tmp != '') { alert(tmp)}">
     Check Fullname </button>
    </body>
    </html>

  • #9
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,711
    Thanks
    41
    Thanked 191 Times in 190 Posts
    i think this would be easier to do with multiple boxes... idk the code off the top of my head, but my approach would be ("planning stage" for a one text box field)
    1) take the text and set a pointer to [0] in the string
    2) first check that all chars in the string are either upper/lower case and contain only "."s
    3) step through the string and copy "chunks" of it to three strings (determined by checking for white space)
    4) validate the string accordingly

    so to try and "demo" what I am saying is if I enter
    Dr. Higgly James Wiggly the 3rd
    this would "chunk out" to be 6 strings... you will need to tell the user he/she has too many. so then they enter
    Higgly J. Wiggly
    now I get three strings and validate each one. I can validate the first as being Higgly (and correct) I can validate the second as J. (and allow this bc it is the "middle name" string) and validate the last as Wiggly (and correct)...
    this will require some nested loops though and will get a little "fun".... wouldn't it be easier to have either three boxes? or have them click a button that stores the name and they do it three times?

    in addition- if you parse out the string to three separate ones can't you just check to verify that at least two of those strings have more than 2 letters and then flip a BOOL variable?
    and finally, you want at least two, but you don't limit... so i can enter
    Dr. Higgly James Wiggly III King of Ireland Duchess of walmart
    ... now what do you do? IMO, direct the user, the more "leniency" you give them, the more problems you will encounter

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Lightbulb

    Quote Originally Posted by clhmedia View Post
    Hi, i know i should have 2 fields for a first and last name in my form but i dont - and was wondering if someone would be able to help me with - or has - a java script valadation rule to only allow...
    Quote Originally Posted by alykins View Post
    i think this would be easier to do with multiple boxes... idk the code off the top of my head, but my approach would be ("planning stage" for a one text box field)
    ...
    I would agree that specific entry is better and easier to code than non-specific.
    I was just trying to answer the OP's original request.


  •  

    Tags for this Thread

    Posting Permissions

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