Here is my React code that can do the conversion but I need a bit of modification in the current approach is The conversion should be as and when I keep typing.
Below is my current code....
import React, { useEffect, useState } from 'react';
import './styles.css';
export default function App() {
const [keyword, setKeyword] = useState('');
const formatInput = (e) => {
setKeyword(e.target.value);
};
const normalize = (keyword) => {
if (keyword.length <= 13) {
setKeyword(keyword.replace(/([A-Z]{2})([A-Z]{3})(\d{6}$)/, '$1-$2-$3'));
}
return null;
};
useEffect(() => {
normalize(keyword);
});
return (
<div className="App">
<input type="text" placeholder="enter string" onChange={formatInput} />
{keyword}
</div>
);
}
// DL-HGK-123456
Expected out should be as below...
For Example:
DLHGK to be converted to Dl-HGK
DLHGK12 to be converted to DL-HGK-12
as an when we are typing the input in our input box.
Thanks for the help.