so I've been having a rough time on how can I properly use regex, I'm creating a register form wherein the email used must consist @pin.edu.sh
. so if the user decided to use for example.
[email protected]
, it will not accept, but if the user uses [email protected]
it will be accepted.
I tried using this, but I think I got it wrong.
/^[a-zA-Z] [a-zA-Z0-9_.] @pin [.] edu [.] sh$/
So I'm using react-hook-form for validation.
const validEmail = /^[a-zA-Z] [a-zA-Z0-9_.] @pin [.] edu [.] sh$/
<TextField
variant="outlined"
name="email"
label="Email"
required
fullWidth
type="email"
autoComplete='email'
{...register('email', {required: "Required", pattern: {value: validEmail, message: "It should be a valid email address!" }})}
error={!!errors?.email}
helperText={errors?.email ?errors.email.message: null}
/>
Additional:
So for example, if the user tried to register using a different email that doesn't contain @pin.edu.sh
for example, [email protected]
, My register form will show that the email used is not allowed.
but if the user used an email like this. [email protected]
, then the register form will accept the account.
CodePudding user response:
You can try below regex
^[\w. \-] @pin\.edu\.sh$
or
^[a-z0-9](\.?[a-z0-9]) @pin\.edu\.sh$
And you can also check both the regex from this website:
CodePudding user response:
You can use the code below to fix the issue in bash.
email="[email protected]"
if [[ $email =~ ^[a-zA-Z0-9._% -] @pin\.edu\.sh$ ]]; then
echo "Valid email address." else
echo "Invalid email address."
fi
CodePudding user response:
A variable named str
and assigns the value [email protected]
or [email protected]
in it. Then, a new regular expression object is generated and assigned to the res
variable.
The regular expression is configured to match the text /^[\w. \-] @pin\.edu\.sh$/
and the g
flag is included as a second input. The g
flag denotes global
in regular expressions. When the g
option is provided, the regular expression searches for all matches of the specified pattern.
This option tells the regular expression to look for all matches to the provided pattern, which is /^[\w. \-] @pin\.edu\.sh$/
, Then, the test()
method is called on the res
regular expression object and passed the str
variable as an argument. The test()
method returns a Boolean value indicating whether or not the regular expression found a match in the given str
.
var str = "[email protected]";
var res = new RegExp(/^[\w. \-] @pin\.edu\.sh$/,"g");
res = res.test(str);
console.log(`[email protected] is ${res}`)
var str = "[email protected]";
var res = new RegExp(/^[\w. \-] @pin\.edu\.sh$/,"g");
res = res.test(str);
console.log(`[email protected] is ${res}`)
CodePudding user response:
I think Benze provided a valuable output above, you can also use this for PY. I'm what was provided to you previously should fix it.
import re
email = "[email protected]"
if re.search(r"@pin\.edu\.sh$", email):
print("valid email")
else:
print("invalid email")