Rails: Validation Containing String - ruby-on-rails

I want to make it so a field containing some string like "#" will not be allowed, generating an error. Is there a validation for this? If not, how will I do this?

Take a look at validates_format_of.

Check out this question here:
rails 3 validation of a string

Related

Xtext - Check whether string is URL

I'm completely new to Xtext so thanks in advance for your help.
I have the following:
terminal PATTERN_SRC : STRING '.png';
Pattern: name='pattern:' value=PATTERN_SRC;
I want the user to code it like this:
pattern: (URL to image ending with .png / .jpg / .gif)
Currently I'm checking it like this but this does not work.
Is there a nice way to solve this? Thanks in advance!
Sone remarks:
Don't use the feature name. This is a reserved keyword for calculating unique names of an element in a resource.
STRING is like this: "Sometext".
So what you want is "somefile.png"
What you described is "somefile".png
You could just use STRING and implement a validation rule to ensure your URL is valid.

Rails - Alphanumeric field validation

In rails 4, I need to validate the alphanumeric field which can accept only dot(.), hyphen(-), slash(/) and space in between the characters.
Eg: AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
I have tried with /^[0-9]+#$/ and /^\d+([.,]\d+)?$/ and /^[0-9]+#$/ but it is not working as per the requirement.
Value should be accept as per the examples. Please help me to validate this field.
I think this might help you:
/^[A-Za-z0-9-\/\.\s]+$/
This worked for all the examples you have provided
AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
and rejected when I inserted a character like ? in the middle(HJS23?JKA34).
Update
If you don't want multiline anchors then you can use it like this:
/\A[A-Za-z0-9-\/\.\s]+\z/
You can use this Rubular site to validate your Regex codes.
Try this:
/^[a-zA-Z\d\.\-\/# ]+$/
try this regex
^[a-zA-Z0-9\. /'\-]+$
Hope this will help

Rails: validation for top level domain only

I need some regex or maybe native Rails trick to check if user entered only domain (without "http", "https", "www" and so on.
So, this one would be valid:
google.com.ua
And this would be invalid:
https://www.google.com.ua
Maybe, it can be simplified just to check if string contains only dots and take it like valid one, and if it contains any other characters - block it.
Tell me please what is better to use for such case and what would be regex for it or another decision.
Thanks.
^(?!www\.)[a-zA-Z0-9.-]+$
Try this.See demo.
https://regex101.com/r/wZ0iA3/5

Prevent user from entering white space during form input in asp.net model validation?

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

Find all URL's in a string in rails 3

I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html

Resources