fixed phone validation desktop

This commit is contained in:
Alex
2026-04-13 21:52:53 -06:00
parent 98584f1324
commit c38bb24e94
5 changed files with 74 additions and 4 deletions

View File

@@ -183,7 +183,7 @@ public class Validator {
}
/**
* Checks if the input is a valid phone number in format XXX-XXX-XXXX
* Checks if the input is a valid phone number in format (XXX) XXX-XXXX
* @param value input of string
* @param name name of input
* @return error msg if input is not in valid phone format, otherwise empty
@@ -191,14 +191,14 @@ public class Validator {
public static String isValidPhoneNumber(String value, String name){
String msg = "";
if (value == null) {
msg += name + " must be in format XXX-XXX-XXXX. \n";
msg += name + " must be in format (XXX) XXX-XXXX. \n";
return msg;
}
String regex = "^\\d{3}-\\d{3}-\\d{4}$";
String regex = "^\\(\\d{3}\\) \\d{3}-\\d{4}$";
if (!value.matches(regex)){
msg += name + " must be in format XXX-XXX-XXXX. \n";
msg += name + " must be in format (XXX) XXX-XXXX. \n";
}
return msg;

View File

@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.endpoints.UserApi;
import org.example.petshopdesktop.api.endpoints.CustomerApi;
import org.example.petshopdesktop.auth.UserSession;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TextFieldFormatSupport;
public class StaffEditDialogController {
@@ -47,6 +48,11 @@ public class StaffEditDialogController {
private UserResponse user;
@FXML
void initialize() {
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
}
public void setUser(UserResponse user) {
this.user = user;
String fullName = user.getFullName() == null ? "" : user.getFullName();

View File

@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.endpoints.CustomerApi;
import org.example.petshopdesktop.auth.UserSession;
import org.example.petshopdesktop.Validator;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TextFieldFormatSupport;
public class StaffRegisterDialogController {
@@ -45,6 +46,11 @@ public class StaffRegisterDialogController {
@FXML
private Button btnCreate;
@FXML
void initialize() {
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
}
@FXML
void btnCreateClicked(ActionEvent event) {
lblError.setText("");

View File

@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.dto.supplier.SupplierResponse;
import org.example.petshopdesktop.api.endpoints.SupplierApi;
import org.example.petshopdesktop.models.Supplier;
import org.example.petshopdesktop.util.ActivityLogger;
import org.example.petshopdesktop.util.TextFieldFormatSupport;
public class SupplierDialogController {
@@ -52,6 +53,8 @@ public class SupplierDialogController {
*/
@FXML
void initialize() {
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
//Set up mouse handlers for buttons
btnSave.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override

View File

@@ -0,0 +1,55 @@
package org.example.petshopdesktop.util;
import javafx.application.Platform;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import java.util.function.UnaryOperator;
public class TextFieldFormatSupport {
/**
* Applies a phone number formatter to a TextField.
* The formatter only allows digits and automatically formats the input as (XXX) XXX-XXXX.
*
* @param textField The TextField to apply the formatter to.
*/
public static void applyPhoneNumberFormat(TextField textField) {
textField.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) return;
// Remove all non-digit characters
String digits = newValue.replaceAll("\\D", "");
// Limit to 10 digits
if (digits.length() > 10) {
digits = digits.substring(0, 10);
}
StringBuilder formatted = new StringBuilder();
int len = digits.length();
if (len > 0) {
formatted.append("(");
if (len <= 3) {
formatted.append(digits);
} else {
formatted.append(digits, 0, 3).append(") ");
if (len <= 6) {
formatted.append(digits.substring(3));
} else {
formatted.append(digits, 3, 6).append("-").append(digits.substring(6));
}
}
}
String result = formatted.toString();
if (!result.equals(newValue)) {
Platform.runLater(() -> {
textField.setText(result);
textField.positionCaret(result.length());
});
}
});
}
}