I am looking for a way to limit users from inputting more than two words, but still allow words with a dash in them.
Allowed
Mike-David Smith
Not Allowed
Mike David Smith
The code I have only restricts input to two words and does not consider words with dashes as single words. Here is the current code:
<script type="text/javascript">//<![CDATA[
$(function(){
var maxWords = 2;
jQuery('#fullname').keypress(function() {
var $this, wordcount;
$this = $(this);
wordcount = $this.val().split(/\b[\s,\.-:;]*/).length;
if (wordcount > maxWords) {
alert("Please enter a maximum of 2 words (forename and surname)");
return false;
}
});
jQuery('#fullname').change(function() {
var words = $(this).val().split(/\b[\s,\.-:;]*/);
if (words.length > maxWords) {
words.splice(maxWords);
$(this).val('');
////////// alert("Please enter a maximum of 2 words (forename and surname)");
}
});
});//]]>
</script>
Your help is greatly appreciated. Thank you!