...

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



clhmedia
07-18-2011, 03:39 AM
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,}/

jmrker
07-18-2011, 05:30 AM
See if this works for you...


<!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>

clhmedia
07-19-2011, 03:35 AM
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

Lerura
07-19-2011, 04:00 AM
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');
}

jmrker
07-19-2011, 04:26 AM
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,}/


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

clhmedia
07-19-2011, 04:29 PM
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.

jmrker
07-19-2011, 04:47 PM
...
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?

jmrker
07-19-2011, 05:11 PM
See if this works any better for you ...


<!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>

alykins
07-19-2011, 05:14 PM
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

jmrker
07-19-2011, 05:46 PM
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...


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum