Flutter email input formatter does not work - regex

I have a Flutter TextFormField for email with input formatter as below.
var emailAddressFormatter = FilteringTextInputFormatter.allow(RegExp(
r"[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+"));
The problem is, when trying to input any character in the field it does not allow. The regex looks fine to me. When the formatter is removed the field accepts any character with any format. What am I going wrong?

The issue is that the FilteringTextInputFormatter that you're using rejects anything that does not match your regex. When you enter just a single character, it does not match your regex, so the character is rejected.
I know little about regex so I'm not sure if it's possible, but you would need a regex that would be able to match every string as you type e.g. a, am, amani#, amani#gmail.com.
I would personally not try to do filtering such as this. Instead, I would just allow all valid characters that are valid in email addresses to be present in the email and not enforce the specific format with the # and .. Then I would use a validator to check that the email is valid upon form submission.
If you don't like the alternate solution I proposed above and you can't use regex, you can make your own input formatter quite easily with TextInputFormatter.withFunction.

Related

Regex to prevent the entering of white space? [duplicate]

I have bee using model validation in asp.net MVC website.
I want to have a functionality to prevent user from entering whitespace in testbox and submit the form.
There are other validation attributes available, but i could not find any validation attribute that prevents user from entering only whitespace in the input textbox.
I could develop a custom attribute for this, but there is another method called regular expression validator which i think i could use easily to achieve this functionality.
For example: We can set an attribute that has a regular expression for validating email. if User enters wrong email, immediately a message is shown that email format is wrong.
I want use the same, but i don't know the regular expresison that validates a form input field if user enters only whitespace.
Please help me with this kind of regular expression?
Thanks,
[RegularExpression(#"[^\s]+")]
public string Data { get; set; }
Use Regex validation with this pattern:
^\S+$
This will allow only non-white-space.
(Update)
If you want users to enter whitespace but only if there are non-whitespace in there:
\S+
This regular expression might work
^[a-zA-Z0-9,-.#~!#$%&*<>?:;_='/()]+(\\s+[a-zA-Z0-9,-.#~!#$%&*<>?:;_='/()]+)*$

Regular Expressions - Exclude characters

I am trying to use regulare expressions with a formvalidator.net to validate a form but I am struggling to under stand regular expressions,I have had a look at some websites but it is still baffling.
Basically I want to validate a field so that when a user enter something it will chack that the word or entry doesnt contain certain characters. So for example I don't want '!name' or 'this ! name' to be accepted.
using regexp for testing I have created this expression ([a-zA-Z\+\&\,\.\-\(\)\d][^<>%$!]+) which excludes those characters but in my form validator it will still accept '!name' or 'this ! name'
Can anyone point me in the right direction? I feel like I'm typing stuff in and gettign nowhere.
Ian
Basically I think you want to either decide what characters you don't want OR what characters you do. Not both.
I think this is what your after:
^[^<>%$!]+$
See it working here

Regex for Valid Email address or Valid alpha numeric check

Hi i have field called UserName into my MVC 3 Model. This propery could be valid email address or could be valid AlphaNumeric. How i can write Regex Express to check both things (Valid Email or Valid Alpha Numeric).
Valid Email Expresion which i am using is
"^[_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))$"
Assuming that the alphanumeric string you want to match instead of an email address really is just A-Z and 0-9, you can take what you have already...
^[_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))$
...and wrap it in parenthesis with a pipe to indicate it as an optional group...
^([_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))|[a-zA-Z0-9]+)$
Which seems like an overly simplistic solution, give the work you have done so far, which makes me wonder if there is some catch to this???
if you want alpha numeric(must contain single alpha and single numeric) try this
(?=[0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*)(?=[0-9a-zA-Z]*[0-9][0-9a-zA-Z]*)(^[0-9a-zA-Z]+$)

regular expression attribute in MVC3

How can I use regular expression attribute in MVC3 on EMAIL field to give an error message if the email entered contains no-email.com?
The exact syntax will depend on the language you are using and possibly the method you are using. These examples should help.
You wouldn't normally need a regular expression to match a simple string.
But, if for some reason, it has to be regex, you would just need to escape the hyphen and dot. Like so:
no\-email\.com
Depending on what you are doing, you may need to match the rest of the email address:
(.*?)no\-email\.com
You may also want to tie "no-email.com" to the end of the string, like so:
(.*?)no\-email\.com$
If you also want to match the # sign to the domain name, do:
(.*?)#no\-email\.com$

Regular expression for length validation in email address

We have a textbox which accepts comma separated email address. The regular expression is
^(\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,]?\b)*$.
However now I am unable to add a length validation to this expression. Each email address should not be more than 150 characters. I have tried
^((\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)){1,150}\s*[,]?\b)*$.
But it doesn't work.
Can you please modify the regex so that it also implements this validation too.
Why use a regexp when you can simply check the length of the string? If you must, use a second regexp:
^.{1,150}$
Use a lookahead
/(?=^.{1,150}$)(your regex goes here)/
I would rather not complicate this regex further and add explicit length check before checking that e-mail matches. In JavaScript it will be something like the following:
function validateEmail(email) {
var regex = /^(\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,]?\b)*$/;
// 20 is used instead of 150 for sake of example
return email.length <= 20 && regex.test(email);
}
// validateEmail("jdoe#example.com") == true
// validateEmail("loooooooooooooooooooonjohn#example.com") == false
By the way, dot after $ in your regex seems to be a mistake, so I skipped it in my snippet.
^([a-zA-Z][\w\.-]*[a-zA-Z0-9]#[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){0,70}$
I arrived here looking for a way to extend my ng-pattern for e-mail for an Angular form.
Taking Rorick's approach of checking for length separately, I found a much easier solution: just set ng-maxlength="150" in the input. Then you can customize an error message that tells the user that the e-mail is too long.
maxlength="150" works fine too, preventing any extra characters from being added to the field, but I liked that ng-maxlength tells you what's wrong rather than truncating your string.