Regular expression to validate a name
You can use
^[A-Z](?=.{1,29}$)[A-Za-z]*(?:\h+[A-Z][A-Za-z]*)*$
The pattern matches:
^
Start of string[A-Z]
Match an uppercase char A-Z(?=.{1,29}$)
Assert 1-29 chars to the right till the end of the string[A-Za-z]*
Optionally match a char A-Za-z(?:\h+[A-Z][A-Za-z]*)*
Optionally repeat 1+ horizontal whitespace chars followed by again an uppercase char A-Z and optional chars A-Za-z$
End of string
Regex demo
In Java with the doubled backslashes
String regex = "^[A-Z](?=.{1,29}$)[A-Za-z]*(?:\\h+[A-Z][A-Za-z]*)*$";
Regex for names validation allow only letters and spaces
To match the terms:
- Expression can start only with a letter
- Expression can contain letters or spaces
You need to use this regex expression:
/^[a-z][a-z\s]*$/
So in your js it should be:
jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-z][a-z\s]*$/i.test(value);
}, "letters only");
Explanation
^[a-z]
means start with one letter[a-z\s]*$
means after accept zero or more letters or spaces
Valid sentence
If you want a valid sentence structure:
- Expression can start or end only with a letter
- Expression cannot contain consecutive spaces
use:
/^([a-z]+\s)*[a-z]+$/
By the way
- These regex expressions do not accept capital letters. To add capital letters support instead of
a-z
usea-zA-Z
Regex to accept only alphabets and spaces and disallowing spaces at the beginning and the end of the string
For me the only logical way to do this is:
^\p{L}+(?: \p{L}+)*$
At the start of the string there must be at least one letter. (I replaced your [a-zA-Z]
by the Unicode code property for letters \p{L}
). Then there can be a space followed by at least one letter, this part can be repeated.
\p{L}
: any kind of letter from any language. See regular-expressions.info
The problem in your expression ^(?!\s*$)
is, that lookahead will fail, if there is only whitespace till the end of the string. If you want to disallow leading whitespace, just remove the end of string anchor inside the lookahead ==> ^(?!\s)[-a-zA-Z ]*$
. But this still allows the string to end with whitespace. To avoid this look back at the end of the string ^(?!\s)[-a-zA-Z ]*(?<!\s)$
. But I think for this task a look around is not needed.
Regex for validating a name of this particular format
use:
^[^\s][ A-Za-z-'@.,/]*$
^[^\s]
checks that there is no whitespace at the beginningA-Za-z
accepts all the alphabets in the given string-'@.,/
accepts only these special characters*
matches 0 or more preceding token
java regex for alpha and spaces is including [ ] \
You have a typo in your regex:
"[a-zA-z\\s]*"
[A-z]
matches all the uppercase and lowercase letters plus [
, ]
, ^
, _
, backslash and backtick, whose code points happen to lie between Z
and a
.
Also, I agree with @Ωmega that you probably should be using an actual space character instead of \s
:
"[a-zA-Z ]*"
The anchors (^
and $
) aren't necessary since you're using the matches()
method, which automatically anchors the match at both ends. They don't hurt anything though, and they do help communicate your intent.
Related Topics
Java Spring Boot Test: How to Exclude Java Configuration Class from Test Context
How to Find Distinct Rows With Field in List Using JPA and Spring
How to Get the Average of a 2D Array
How to Go About Adding an Image into a Java Project With Eclipse
How to Pass or Send Data from Recyclerview Adapter to Fragment
How to Get Multiple Columns from Table Using Jpa
Set Current Timezone to @Jsonformat Timezone Value
Spring @Requestbody and Enum Value
Spring Security - 405 Request Method 'Post' Not Supported
The Request Was Rejected Because No Multipart Boundary Was Found in Springboot
Find Difference Between Two Strings
How to Get All Keys from a Json-Object as a String Array in Java
How to Update Only the Incoming Fields in a Model Using Spring Data Jpa
Java.Lang.Noclassdeffounderror: Org/Springframework/Core/Env/Configurableenvironment
Tomcat Server Error - Port 8080 Already in Use
How to Fix Expected Begin_Object But Was String in Retrofit
Error: Incompatible Types: List<Integer> Cannot Be Converted to Arraylist<Integer>
Convert Multi If-Else to Simple Lambda Expression Code in Java