Skip to content

Commit 8e93559

Browse files
Merge pull request #93 from BahmniIndiaDistro/BAH-4138
BAH- 4138 | Add validation for custom ABHA Address input
2 parents bd4dd08 + 6c19a6e commit 8e93559

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/components/creation/CreateABHAAddress.jsx

+33-11
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,53 @@ const CreateABHAAddress = (props) => {
1717
props.setNewAbhaAddress,
1818
];
1919
const cmSuffix = localStorage.getItem(cmSuffixProperty);
20-
const [lodingAbhaAddressSuggestions, setLodingAbhaAddressSuggestions] = useState(false);
20+
const [loadingAbhaAddressSuggestions, setLoadingAbhaAddressSuggestions] = useState(false);
2121
const [abhaAddressSuggestions, setAbhaAddressSuggestions] = useState([]);
2222

2323
useEffect(async () => {
24-
setLodingAbhaAddressSuggestions(true);
24+
setLoadingAbhaAddressSuggestions(true);
2525
let response = await getAbhaAddressSuggestions();
2626
if (response.data !== undefined) {
27-
setLodingAbhaAddressSuggestions(false);
27+
setLoadingAbhaAddressSuggestions(false);
2828
const fetchedOptions = response.data.abhaAddressList.map((item) => ({
2929
label: item,
3030
value: item,
3131
}));
3232
setAbhaAddressSuggestions(fetchedOptions);
3333
} else {
34-
setLodingAbhaAddressSuggestions(false);
34+
setLoadingAbhaAddressSuggestions(false);
3535
console.error("An error occurred while getting suggestions");
3636
}
3737
}, []);
3838

39+
const validateAbhaAddress = (newAbhaAddress) => {
40+
const MIN_LENGTH = 8;
41+
const MAX_LENGTH = 18;
42+
43+
if (newAbhaAddress.length < MIN_LENGTH || newAbhaAddress.length > MAX_LENGTH) {
44+
return `ABHA Address must be between ${MIN_LENGTH} and ${MAX_LENGTH} characters.`;
45+
}
46+
47+
const abhaRegex = /^(?![._])(?!.*\..*\..*)(?!.*_.*_.*)[a-zA-Z0-9._]*(?<![._])$/;
48+
49+
if (!abhaRegex.test(newAbhaAddress)) {
50+
return (
51+
<div>
52+
ABHA Address can only contain letters, numbers, a dot (.) or an underscore (_).
53+
<br />
54+
The dot (.) or underscore (_) must not be at the beginning or end and should not appear consecutively.
55+
</div>
56+
);
57+
}
58+
return null;
59+
};
60+
3961
async function onCreate() {
4062
setError("");
41-
if (newAbhaAddress === "") {
42-
setError("ABHA Address cannot be empty");
43-
} else if (newAbhaAddress.length > 3) {
63+
const validationError = validateAbhaAddress(newAbhaAddress);
64+
if (validationError) {
65+
setError(validationError);
66+
} else {
4467
setLoader(true);
4568
var response = await createABHAAddress(newAbhaAddress);
4669
setLoader(false);
@@ -50,8 +73,6 @@ const CreateABHAAddress = (props) => {
5073
setNewAbhaAddress(newAbhaAddress.concat(cmSuffix));
5174
props.setABHAAddressCreated(true);
5275
}
53-
} else {
54-
setError("ABHA Address should have minimum of 4 characters");
5576
}
5677
}
5778

@@ -65,7 +86,7 @@ const CreateABHAAddress = (props) => {
6586
id="free-solo-demo"
6687
freeSolo
6788
options={abhaAddressSuggestions.map((option) => option.label)}
68-
loading={lodingAbhaAddressSuggestions}
89+
loading={loadingAbhaAddressSuggestions}
6990
inputValue={newAbhaAddress}
7091
onInputChange={(event, value) => setNewAbhaAddress(value)}
7192
renderInput={(params) => (
@@ -80,7 +101,7 @@ const CreateABHAAddress = (props) => {
80101
),
81102
}}
82103
noOptionsText={
83-
lodingAbhaAddressSuggestions
104+
loadingAbhaAddressSuggestions
84105
? "Getting suggestions..."
85106
: "No suggestions"
86107
}
@@ -108,3 +129,4 @@ const CreateABHAAddress = (props) => {
108129
};
109130

110131
export default CreateABHAAddress;
132+

0 commit comments

Comments
 (0)