我正在尝试使用正则表达式
pattern
将 input
验证添加到我的 ^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$
中,并且在输入我希望有效的 AB1 2CD
形式的邮政编码后,我不断收到无效消息。 .
const REGEXES = {
postcode: /^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/,
...
};
const ADDRESS_FORM_FIELDS = [
...,
{
name: 'postcode',
required: true,
label: 'Postcode',
type: 'text',
pattern: REGEXES.postcode
}
];
const MyForm = () => {
...
return (
<form ...>
{
ADDRESS_FORM_FIELDS.map(({name, required, label, type, ...rest}) => (<>
<label for={name}>{label}{required ? ' *' : ''}</label>
<input {...{name, type, required, placeholder: label, ...rest, value: addressValues[name], onChange: handleAddressFormChange}} />
</>))
}
</form>
)
}