Validating Patterns with Regular Expressions
Learn how to validate fields against a custom format using regular expressions.
We'll cover the following...
We'll cover the following...
Regular expressions define search patterns that can be used for validation purposes. In this lesson, we will show how to validate a currency input to match our custom format. We will create a field prizeMoney
to store the total earnings of the player. The entered value has to follow the $#,###,###
format. Values that do not match this format will result in a validation error.
First step is to create the field for prize money along with the getter and setter methods.
public class Athlete {//...private String prizeMoney;//...public String getPrizeMoney() {return prizeMoney;}public void setPrizeMoney(String prizeMoney) {this.prizeMoney = prizeMoney;}}
Adding the prizeMoney field to Athlete class
...