I would like to validate email address from user giving information to my chatbot.
Is there a simple way to deny unexpected symbols with regular expressions and CSML?
Here's my current code:
askEmail:
say "Thank you {{firstname}}"
say "What is your email address?"
hold
remember email = event
if (Find("#", in="email")) goto askCompany
else {
say "Please enter a real email address"
goto askEmail
}
I need to replace Find with a function using regexp and check all unexpected symbols.
There is a contains_regex string method in CSML that you can use to verify more complex rules than just "is there a # in this string?".
It's quite hard to cover all possible valid email addresses with a regex (see https://emailregex.com), but let's just agree for your purpose that emails are in the general form of "whatever#whatever.whatever" where "whatever" is any string that does not contain any line break.
This is obviously not a 100% foolproof regex as it is a bit naive, but validating emails is still a Hard Thing ®. Good enough is usually good enough.
Here goes:
askEmail:
say "Thank you {{firstname}}"
say "What is your email address?"
hold
remember email = event
if (email.contains_regex("^.+#.+\..+$")) goto askCompany
else {
say "Please enter a real email address"
goto askEmail
}
You can obviously tweak the regex to your liking!
emailPattern = /^([a-z\d]{1})([\w-\.!#$%&'*+\/=?\^`\{\|\}~"\(\),:;<>\[\\\]]){2,}([a-z\d]{1})#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i;
console.log('Regex test is: ', emailPattern.test('test#test.test'));
emailPattern = /^([a-z\d]{1})([\w-\.!#$%&'*+\/=?\^`\{\|\}~"\(\),:;<>\[\\\]]){2,}([a-z\d]{1})#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i;
Email Regular Expression Pattern
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* #[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
Related
recently, I started playing with Django and created a custom form for user registration. In that form to create the field for email I use something like
email = forms.EmailField()
I observed that address such as a#a.a is considered invalid by the form. Of course this is a nonsense email address. Nonetheless, I wonder how does Django checks for validity.
I found some topics on the net discussing how to check for validity of an email address but all of them were providing some custom ways. Couldn't find something talking about the django default validator.
In their docs on the email filed they specify
Uses EmailValidator to validate that the given value is a valid email address, using a moderately complex regular expression.
However that's not very specific so I decided to ask here.
For anyone also interested in this, I would suggest looking up the implementation (django.core.validators) as was kindly suggested by iklinac in the comments.
In it, there is not just the source but also mentions about standards that were used to derive regexes that check if domain and literal have valid format.
us should check docs here https://www.geeksforgeeks.org/emailfield-django-forms/#:~:text=EmailField%20in%20Django%20Forms%20is,max_length%20and%20min_length%20are%20provided.
if u wanna check validation use clean function like this :
from django.forms.fields import EmailField
email = EmailField()
my_email = "a#a.a"
print(email.clean(my_email))
if your email is valid then this func return value else return validation error
I have a Cucumber step that looks like this:
When I enter the credentials for the user
and another that says
When I enter the correct credentials for the user
Corresponding step definitions are:
#When("I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
#When("I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
But my second cucumber step ("I enter the correct credentials for the user") is matched by the first step definition, only with the word "correct" added to the credentials.
How do I fix this?
I'm new to regex. Would it be possible to exclude the "correct" part from the 'When' step so I could have a basic step that could be 'extended' with the "correct" part?
The first rule should be changed to
#When("I enter the (\\S+) for the user$")
Here, \S+ matches 1 or more non-whitespace characters. If there can be no non-whitespace chars use \S*.
To match two "words" you may use
#When("I enter the (\\S+\\s+\\S+) for the user$")
Note that you may control the number of "words" using quantifiers, e.g. this will match 2 or 3 words:
#When("I enter the (\\S+(?:\\s+\\S+){1,2}) for the user$")
To match 2 or more words:
#When("I enter the (\\S+(?:\\s+\\S+){1,}) for the user$")
#When("I enter the (\\S+(?:\\s+\\S+)+) for the user$")
There are a couple of ways you could improve these steps and avoid the use of regex.
1) Have the user know its credentials and have the step ask the user for the credentials
So you would have
Given I am a user
#user = create_user # method creates a user with credentials
end
When `I enter the users credentials` do
fill_in username: #user.username
fill_in password: #user.password
end
When `I enter the wrong credentials for the user` do
fill_in username: #user.username
fill_in password: #user.bad_password # or perhaps just bad_password
end
this approach removes all the complexity from cucumber and places it in the helper methods you are calling to create a user.
2) Have more arguments for your step definition
When 'I enter the credentials user: (\\S+) password: (\\S+) do |username, password|
fill_in username: username
fill_in password: password
end
When 'I enter the bad credentials user: (\\S+) password: (\\S+) do |username, password|
fill_in username: username
fill_in password: password
end
I strongly prefer the first approach, you should keep features and scenarios super simple and push complexity down to code. Code is much better at dealing with complexity than Cucumber.
I've been cuking since before Cucumber was named, and now never use regex's or scenario outlines when I cuke. You don't need to either.
Several answers suggest an imperative approach which is considered an anti-pattern in BDD. Instead I strong suggest you follow the declarative approach with your Gherkin using natural or business language. If you are actually testing the login feature, I'd propose something like:
When an authorised user enters their credentials
or role based
When an Administrator is authorised
If login is actually a prerequisite for the feature under test then something such as:
Given an authorised user
or
Given an authorised Administrator
These can be backed up with a Credentials Manager.
... = ExpectedData.credentialsFor("#authorised");
The tag should represent the characteristics, not the identity of the expected data, to be retrieved from a test data db or csv containing something like:
#admin, administrator, password
#authorised, user, password
#unauthorised, user, wrong
The same approach should be used for all test data entry, such as:
Given a Cash Customer
Given a Credit Customer
Given a Customer with an overdue account
A strong benefit of this approach is the test suit can be readily reused on different environment by making the data/credential handler environment aware.
Please execute below step definitions and let us know if worked for you.
#When("^I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
#When("^I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
Without Parameter
With Parameter
Two metacharacters (^, $) are called anchors, because they’re used to tie down each
end of the regular expression to the beginning and end of the string that they
match on.
I'm writing a .NET MVC application and using unobtrusive validation to sanitize my client inputs based on data annotations in my model. I have an input that I do not want to allow HTML tags into and would like to display a custom error message if an html tag is entered. As such I have created a data annotation with a custom regex expression to cover these conditions, like so:
[Required(ErrorMessage = "You must provide a First Name.")]
[RegularExpression(#"<[a-z][\s\S]*>", ErrorMessage = "Invalid character")]
[DisplayName("First Name")]
public string FirstName { get; set; }
The issue with this is, no matter what character, whether it be <test> or whether it be abc will cause the Invalid Character message to appear. The required attribute works fine, and if I try a simple regex such as:
[RegularExpression("[a-z]", ErrorMessage = "Invalid character")]
This works 100% as expected, leading me to believe my regex is incorrect, nut I know it works for HTML validation as I can prove it out with online tools. What am I doing wrong?
If you take a look at the documentation of the RegularExpressionAttribute, it states:
Specifies that a data field value in ASP.NET Dynamic Data must match the specified regular expression.
So your attribute is doing the exact opposite of what you want to do is:
[RegularExpression(#"^(?!.*<.*>).*$", ErrorMessage = "Invalid character")]
I am struggling with writing a regex for validating email address for only one domain.
I have this expression
[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}
But the issue is that for example hello#gmail.com.net is valid and I only want to be only valid for only one domain. So hence I do not want hello#gmail.com.net to be valid.
Help is needed. Thank you!
try this [A-Z0-9a-z._%+-]+#[A-Za-z0-9-]+\.[A-Za-z]{2,64}.
In your regex is a dot in the allowed characters behind the #.
You can use something like:
\b[A-Z0-9a-z._%+-]+#gmail\.com\.net\b
Regex Demo
I found this regex for Swift:
[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}
It has an extra backslash.
I found it here: http://emailregex.com/
Regards,
Melle
I know you already accept an answer but this idea just cross my mind. You can use URLComponents to split the email address into user and host and validate each component separately:
func validate(emailAddress: String) -> Bool {
guard let components = URLComponents(string: "mailto://" + emailAddress),
let host = components.host else
{
return false
}
return host.components(separatedBy: ".").count == 2
}
print(validate(emailAddress: "hello#gmail.com")) // true
print(validate(emailAddress: "hello#gmail.com.net")) // false
print(validate(emailAddress: "hello")) // false
Your requirement has a big flaw in it though: valid domains can have two dots, like someone#bbc.co.uk. Getting a regex pattern to validate an email is hard. Gmail, for example, will direct all emails sent to jsmith+abc#gmail.com to the same inbox as jsmith#gmail.com. The best way is to perform some rudimentary check on the email address, then email the user and ask them to click a link to confirm the email.
You can try with below pattern.
/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#((REPLACE_THIS_WITH_EMAIL_DOMAIN+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i;
for Eg.
/^(([^<>()\[\]\.,;:\s#\"]+(\.[^<>()\[\]\.,;:\s#\"]+)*)|(\".+\"))#((gmail+\.)+[^<>()[\]\.,;:\s#\"]{2,})$/i;
I am trying to add validation, inside my User model to validation emails using regex.
However, it's spits a dummy out at the first apostrophe.
'email' => 'required|regex:/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/',
Have you tried the 'email' validation rule?
'email' => 'required|email|unique:users,email'
http://laravel.com/docs/4.2/validation#rule-email
As the answer to this question on SO states, there is no simple regular expression to validate an email-address. Using your RegEx could maybe catch valid addresses (although that's just speculation of mine). Using the email-validation-rule would be my first choice.
But you are right, this is just the server side in the first place, if you ignore redirecting users back with input and error messages..
On the client-side, you would have some options. The first one would be to simply rely on the build in browser-validation, by declaring the corresponding input-field as an email-address which you should do anyway:
{{ Form::email($name, $value = null, $attributes = array()) }}
Another, more advanced way would be to create some kind of helper to check the typed input via Ajax using the same validation rule and returning the error messages or sth. similar. This could be an additional route to your Model-Resource for example. This way, you would be stable and consistent.