Rails format validation -- alphanumeric, but not purely numeric
You want to 'look ahead' for a letter:
/\A(?=.*[a-z])[a-z\d]+\Z/i
Alphanumeric, dash and underscore but no spaces regular expression check JavaScript
However, the code below allows spaces.
No, it doesn't. However, it will only match on input with a length of 1. For inputs with a length greater than or equal to 1, you need a +
following the character class:
var regexp = /^[a-zA-Z0-9-_]+$/;
var check = "checkme";
if (check.search(regexp) === -1)
{ alert('invalid'); }
else
{ alert('valid'); }
Note that neither the -
(in this instance) nor the _
need escaping.
Rails using arrays with forms & validation inclusion of
You can add these two constants to your model and then call the validation:
VALID_STATES = ["live", "paused"]
SELECT_STATES = VALID_STATES.map { |s| [s.capitalize, s] }
validates :status, :inclusion => { :in => Model::VALID_STATES }
Regex to accept alphanumeric and some special character in Javascript?
use:
/^[ A-Za-z0-9_@./#&+-]*$/
You can also use the character class \w to replace A-Za-z0-9_
How to validate phone numbers using regex
Better option... just strip all non-digit characters on input (except 'x' and leading '+' signs), taking care because of the British tendency to write numbers in the non-standard form +44 (0) ...
when asked to use the international prefix (in that specific case, you should discard the (0)
entirely).
Then, you end up with values like:
12345678901
12345678901x1234
345678901x1234
12344678901
12345678901
12345678901
12345678901
+4112345678
+441234567890
Then when you display, reformat to your hearts content. e.g.
1 (234) 567-8901
1 (234) 567-8901 x1234
Regex: How to match a string that is not only numbers
(?!^\d+$)^.+$
This says lookahead for lines that do not contain all digits and match the entire line.
Block typing in any other alphabet than english
I use this function to remove special characters from input
function removeSpecials(evt) {
var input = document.getElementById("myinput");
var patt = /[^\u0000-\u007F ]+/;
setTimeout(function() {
var value = input.value;
input.value = value.replace(patt,"");
},100);
}
The point is: I create a pattern to detect all special characters
var patt = /[^\u0000-\u007F ]+/;;
and I replace the input value
value.replace(patt,"");
Here is a FIDDLE
Should all validation rules be tested within a Cucumber feature?
This is a good question, and the answer is: It depends.
You can think of Cucumber as a way of communicating between the product owner, developers and testers.
If you feel that having the validations in Cucumber adds to the shared understanding of what the product does, then keep them there.
One approach is to combine the validations into a scenario outline:
Scenario Outline: User tries to register but skips a mandatory field
Given I am registering
And I leave the "<field>" blank
When I click "Submit"
Then I should see "<message>"
And I should not be registered
| field | message |
| Forename | Please enter your forename |
| Surname | Please enter your surname |
| Date of Birth | Please enter your date of birth |
Regular Expression to match only alphabetic characters
You may use any of these 2 variants:
/^[A-Z]+$/i
/^[A-Za-z]+$/
to match an input string of ASCII alphabets.
[A-Za-z]
will match all the alphabets (both lowercase and uppercase).^
and$
will make sure that nothing but these alphabets will be matched.
Code:
preg_match('/^[A-Z]+$/i', "abcAbc^Xyz", $m);
var_dump($m);
Output:
array(0) {
}
Test case is for OP's comment that he wants to match only if there are 1 or more alphabets present in the input. As you can see in the test case that matches failed because there was ^
in the input string abcAbc^Xyz
.
Note: Please note that the above answer only matches ASCII alphabets and doesn't match Unicode characters. If you want to match Unicode letters then use:
/^\p{L}+$/u
Here, \p{L}
matches any kind of letter from any language
Related Topics
Require Command Not Working Within Bash Irb on Snow Leopard
How to Install Rmagick Gem on Windows
Remove All Data from Active Storage
Flash Message with HTML_Safe from the Controller in Rails 4 (Safe Version)
Encoding Issues in JavaScript Files Using Rails Asset Pipeline
Unexpected Value of _Callee_ When Including a Module - Is This a Ruby Bug
How to Raise an Exception in an Rspec Test
Are There More Elegant Ways to Prevent Negative Numbers in Ruby
Ruby - Compare Two Enumerators Elegantly
How to Call Methods Defined in Applicationcontroller in Models
Argumenterror: Wrong Number of Arguments (1 for 2)
Ruby - What's the Difference Between Single and Double Quotes
Why Does Date.Yesterday Counts as Date.Today Also
How to Set Up the Recipient Id in Public Activity
Fast-Stemmer Installation Problems
How to Pass Content to Jekyll Default Converter After Custom Conversion