Rails Format Validation -- Alphanumeric, But Not Purely Numeric

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



Leave a reply



Submit