Skip to content

Commit ecb1eb1

Browse files
committed
Username allowed characters
1 parent df3c4fd commit ecb1eb1

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/main/java/alexp/blog/model/User.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hibernate.validator.constraints.NotBlank;
55

66
import javax.persistence.*;
7+
import javax.validation.constraints.Pattern;
78
import javax.validation.constraints.Size;
89
import java.util.ArrayList;
910
import java.util.List;
@@ -27,6 +28,7 @@ public interface ChangePasswordValidationGroup {}
2728
@Size(max = 25, message = "Username too long", groups = {CreateValidationGroup.class})
2829
})
2930
@NotBlank(groups = {CreateValidationGroup.class})
31+
@Pattern(regexp = "^[\\p{L}0-9\\._\\- ]+$", groups = {CreateValidationGroup.class})
3032
private String username;
3133

3234
@Column(unique = true, nullable = false, length = 50)

src/main/webapp/WEB-INF/templates/registration.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
layout:decorator="layouts/blog">
88

99
<head>
10+
<script src="../../js/xregexp-all-min.js" th:src="@{/js/xregexp-all-min.js}"></script>
1011
<script src="../../js/registration.js" th:src="@{/js/registration.js}"></script>
1112
</head>
1213

src/main/webapp/js/registration.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ $(document).ready(function() {
1515
return "pending";
1616
};
1717

18+
$.validator.addMethod("username", function(value, element) {
19+
return this.optional( element ) || XRegExp("^[\\p{L}0-9\\._\\- ]+$").test( value );
20+
}, 'Only letters, numbers, space, ".", "-" and "_" are allowed.');
21+
1822
$("#regForm").validate({
1923
rules: {
2024
username: {
2125
required: true,
2226
minlength: 3,
27+
username: true,
2328
remote: {
2429
url: window.usernameCheckUrl,
2530
type: "get",

src/main/webapp/js/xregexp-all-min.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)