fixed phone validation desktop
This commit is contained in:
@@ -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 value input of string
|
||||||
* @param name name of input
|
* @param name name of input
|
||||||
* @return error msg if input is not in valid phone format, otherwise empty
|
* @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){
|
public static String isValidPhoneNumber(String value, String name){
|
||||||
String msg = "";
|
String msg = "";
|
||||||
if (value == null) {
|
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;
|
return msg;
|
||||||
}
|
}
|
||||||
String regex = "^\\d{3}-\\d{3}-\\d{4}$";
|
String regex = "^\\(\\d{3}\\) \\d{3}-\\d{4}$";
|
||||||
|
|
||||||
if (!value.matches(regex)){
|
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;
|
return msg;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.endpoints.UserApi;
|
|||||||
import org.example.petshopdesktop.api.endpoints.CustomerApi;
|
import org.example.petshopdesktop.api.endpoints.CustomerApi;
|
||||||
import org.example.petshopdesktop.auth.UserSession;
|
import org.example.petshopdesktop.auth.UserSession;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
import org.example.petshopdesktop.util.TextFieldFormatSupport;
|
||||||
|
|
||||||
public class StaffEditDialogController {
|
public class StaffEditDialogController {
|
||||||
|
|
||||||
@@ -47,6 +48,11 @@ public class StaffEditDialogController {
|
|||||||
|
|
||||||
private UserResponse user;
|
private UserResponse user;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
void initialize() {
|
||||||
|
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
|
||||||
|
}
|
||||||
|
|
||||||
public void setUser(UserResponse user) {
|
public void setUser(UserResponse user) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
String fullName = user.getFullName() == null ? "" : user.getFullName();
|
String fullName = user.getFullName() == null ? "" : user.getFullName();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.endpoints.CustomerApi;
|
|||||||
import org.example.petshopdesktop.auth.UserSession;
|
import org.example.petshopdesktop.auth.UserSession;
|
||||||
import org.example.petshopdesktop.Validator;
|
import org.example.petshopdesktop.Validator;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
import org.example.petshopdesktop.util.TextFieldFormatSupport;
|
||||||
|
|
||||||
public class StaffRegisterDialogController {
|
public class StaffRegisterDialogController {
|
||||||
|
|
||||||
@@ -45,6 +46,11 @@ public class StaffRegisterDialogController {
|
|||||||
@FXML
|
@FXML
|
||||||
private Button btnCreate;
|
private Button btnCreate;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
void initialize() {
|
||||||
|
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
|
||||||
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void btnCreateClicked(ActionEvent event) {
|
void btnCreateClicked(ActionEvent event) {
|
||||||
lblError.setText("");
|
lblError.setText("");
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.example.petshopdesktop.api.dto.supplier.SupplierResponse;
|
|||||||
import org.example.petshopdesktop.api.endpoints.SupplierApi;
|
import org.example.petshopdesktop.api.endpoints.SupplierApi;
|
||||||
import org.example.petshopdesktop.models.Supplier;
|
import org.example.petshopdesktop.models.Supplier;
|
||||||
import org.example.petshopdesktop.util.ActivityLogger;
|
import org.example.petshopdesktop.util.ActivityLogger;
|
||||||
|
import org.example.petshopdesktop.util.TextFieldFormatSupport;
|
||||||
|
|
||||||
public class SupplierDialogController {
|
public class SupplierDialogController {
|
||||||
|
|
||||||
@@ -52,6 +53,8 @@ public class SupplierDialogController {
|
|||||||
*/
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
void initialize() {
|
void initialize() {
|
||||||
|
TextFieldFormatSupport.applyPhoneNumberFormat(txtPhone);
|
||||||
|
|
||||||
//Set up mouse handlers for buttons
|
//Set up mouse handlers for buttons
|
||||||
btnSave.setOnMouseClicked(new EventHandler<MouseEvent>() {
|
btnSave.setOnMouseClicked(new EventHandler<MouseEvent>() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -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());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user