Skip to main content
Participant
April 19, 2017
Answered

How do I configure the form validation to only accept letters for firstname and lastname?

  • April 19, 2017
  • 1 reply
  • 1341 views

I'm working in Dreamweaver CS6 and have a question in regards to the form validation. I already have a hidden input field in place and with the coding in my formmail script it has stopped spam over the years. At the moment however, I'm getting spam where the firstname and lastname fields present with a mixture of numbers and letters.  This spam seems to be only targeting my "Required" fields. Is there a way to specify that the name fields type:text be only letters a to z?

The form validation at the moment is:

<script type="text/javascript">

<!--

function MM_validateForm() { //v4.0

  if (document.getElementById){

    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args);

      if (val) { nm=val.name; if ((val=val.value)!="") {

        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');

          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';

        } else if (test!='R') { num = parseFloat(val);

          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';

          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');

            min=test.substring(8,p); max=test.substring(p+1);

            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';

      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }

    } if (errors) alert('The following error(s) occurred:\n'+errors);

    document.MM_returnValue = (errors == '');

} }

//-->

If anyone can help it's greatly appreciated.

This topic has been closed for replies.
Correct answer Rob Hecker2

You are using machine-generated javascript validation. I use PHP validation, not javascript, and the generated code is not human friendly, so I would not try to modify that function. (but I will provide a solution). Do you understand what I mean? People write code that is easy to understand and modify  by other people. That code isn't written with that objective.

Validation can happen on the form, after the form has been submitted, or both. I will provide a solution that happens on the form.

Remember that names may contain more than just alphabetical characters. They may contain dashes, apostrophes, empty spaces, accented characters and non-latin characters like umlaut (Klein-Smith, O'brien, Del Oro, Züm).

The following example uses a regular expression to match the name. Note that it explicitly includes accented characters for European languages, but it would reject names using, for instance, Vietnamese characters. It will not allow a name with more than 80 characters. This solution requires the HTML5 doctype. It will not work with XHTML or HTML4.

<input type='text' name='lastname' id='lastname' pattern="[A-Za-zéèàùâêîôûçëïüæœÀÂÆÉÈÊËÏÎÔŒÙÛÜŸ'\s*]{1,80}" title='Some characters may be rejected'/>

An alternative method would be to explicitly reject certain characters, such as <>?*&%. Read up on regular expressions if you want to explore that option.

1 reply

Rob Hecker2
Rob Hecker2Correct answer
Legend
April 19, 2017

You are using machine-generated javascript validation. I use PHP validation, not javascript, and the generated code is not human friendly, so I would not try to modify that function. (but I will provide a solution). Do you understand what I mean? People write code that is easy to understand and modify  by other people. That code isn't written with that objective.

Validation can happen on the form, after the form has been submitted, or both. I will provide a solution that happens on the form.

Remember that names may contain more than just alphabetical characters. They may contain dashes, apostrophes, empty spaces, accented characters and non-latin characters like umlaut (Klein-Smith, O'brien, Del Oro, Züm).

The following example uses a regular expression to match the name. Note that it explicitly includes accented characters for European languages, but it would reject names using, for instance, Vietnamese characters. It will not allow a name with more than 80 characters. This solution requires the HTML5 doctype. It will not work with XHTML or HTML4.

<input type='text' name='lastname' id='lastname' pattern="[A-Za-zéèàùâêîôûçëïüæœÀÂÆÉÈÊËÏÎÔŒÙÛÜŸ'\s*]{1,80}" title='Some characters may be rejected'/>

An alternative method would be to explicitly reject certain characters, such as <>?*&%. Read up on regular expressions if you want to explore that option.

pziecina
Legend
April 19, 2017

For client side form validation use the html5 doctype and html5 form elements as these have built-in client side form validation, (just requires you to say what) see -

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Form_validation

You must use server side validation also.