4

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?

3 Answers 3

9

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'- ]+$/ 
Sign up to request clarification or add additional context in comments.

2 Comments

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.).
Add the Character "." using /^[a-zA-Z-'. ]+$/ to allow a user to add a title (like Dr. or Prof.). Makes "Prof. Dr. Hans-Peter O'Donnell" valid.
5

Try this RegEx for maximum compatibility:

Don't forget to escape the single quote-marks (') if you put this in a JavaScript string enclosed with single quotes.

^(?:((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-.\s])){1,}(['’,\-\.]){0,1}){2,}(([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-. ]))*(([ ]+){0,1}(((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){1,})(['’\-,\.]){0,1}){2,}((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){2,})?)*)$

Example:

   function checkName(eid){ alert(/^(?:((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-.\s])){1,}(['’,\-\.]){0,1}){2,}(([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-. ]))*(([ ]+){0,1}(((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){1,})(['’\-,\.]){0,1}){2,}((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){2,})?)*)$/.test(document.getElementById(eid).value)? 'Congratulations! You entered a valid name.' : 'Sorry, You entered an invalid name. Please try again.');};
*
	{
		color:#535353;
		font-family:Arial, Helvetica, Sans-Serif;
	}
input:valid
	{
		background-color: #DEFFDF;
	}

input:invalid
	{
		background-color: #C7D7ED;
	}
<form action="#" method="post" onsubmit="checkName('full_name');return false;">

<label for ="full_name">Your Name: </label>
<input type="text" name="full_name" id="full_name" maxlength="85" pattern="^(?:((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-.\s])){1,}(['’,\-\.]){0,1}){2,}(([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-. ]))*(([ ]+){0,1}(((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){1,})(['’\-,\.]){0,1}){2,}((([^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]'’,\-\.\s])){2,})?)*)$" title="Please enter your FULL name." style='width:200px;height:auto;' required>
<br><br>
<button type="submit">Submit</button>
&nbsp;&nbsp;
<button type="reset">Reset</button>
</form>

Comments

3

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.

2 Comments

Javascript validations are a wonderful complement to server side validations - imho you should strive to have both.
And if you're running Node on backend, you can have the same validators on both sides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.