Tutorial :How can I validate a full name input in a form using javascript?



Question:

I would like to write a function to validate a full name input in a form with Javascript:

a single word will be ok, a string with some blank character among name surname, middle name too, but I do not want any digit.

Any suggestion?


Solution:1

There are several ways to write this, but, simply, the regular expression

/^[a-zA-Z ]+$/   

should work for you. It will find any combination of alpha characters and spaces but no digits.

Edit to add the information from my comment, which I feel is important:

You may also wish to add the apostrophe and hyphen between the brackets, since Irish and Italian names include the former (O'Donnell, D'Ambrosio) and some folks have hyphenated last names (Claude Levi-Strauss, Ima Page-Turner, etc.).

This would result in the following expression:

/^[a-zA-Z'- ]+$/   


Solution:2

I would suggest not putting so much effort in validating data via JS. If a user has JS disabled, you will end up with some data you don't want on database.

Validate it via the server side.

Now, regards your question, I would try with regular expressions.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »