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

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,-.#~!#$%&*<>?:;_='/()]+)*$

Related

Flutter email input formatter does not work

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.

I am using Regex for email validation EMAIL_PATTERN:/^(\w+([.]\w+)*#\w+([.]\w+)*\.\w+([.]\w+)*)$/,

I'm Following the below Example:
Email validation expression \w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)* allows empty spaces
I'm using EMAIL_PATTERN:
`/^(\w+([.]\w+)*#\w+([.]\w+)*\.\w+([.]\w+)*)$/`
with minor changes.
It is allowing me to enter the email as i want but i dont want "_" at the
starting of the email address. Example: _abc#gmail.com
How to solve this?
What regex google is using in gmail?
Use This
/^([a-zA-Z0-9]+([.][a-zA-Z0-9]+)#\w+([.]\w+)\.\w+([.]\w+)*)$/,

How to validate form entry in Ruby on rails validation for specific entry

Hi I want an entry in a form to start a specific way.
I want the entry to begin with "http:abc.com/w/" and there will have to be numbers and letters after. For example "http:abc.com/w/90765g5" is fine.
How do I set up a validation so that the user has to enter "http:abc.com/w/" in the start of the field?
Thanks
Mike
You'll need to use a regex validation, and write a custom regex to match the conditions you want:
validates :my_field, format: { with: /\Ahttp:abc\.com\/w\/([a-z0-9]*)\z/,
message: 'Please make sure it matches the right format'}
BTW, if you're on a mac, I highly recommend this app to help writing regexs: https://itunes.apple.com/au/app/patterns-the-regex-app/id429449079?mt=12

how to improve this short regularexpression to filter words

I am using mvc3 and regular expressions on a field in a model and it works perfectly to prevent certain words from being typed; however I was wondering is there anyway that I can make RegularExpression words not case sensitive: I can block www but if a user types Www it goes through.
[RegularExpression("^((?!(www)).)*$", ErrorMessage = "Invalid character")]
I have other words in that regularexpression but no need to put them there any help would be appreciated..
Use a character class: "^((?!([wW]{3})).)*$"
Also, you can try to add an inline ?i modifier to your pattern to force it to be case insensitive. See here: How to use inline modifiers in C# regex?

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$