I have been looking forever how I can use Regex to validate input for c++ 11. What I basically would like is a piece of code that validates a number input within a specific range (for this example lets say 0-9). It should only allow numbers that I want the user to enter within the range. How can I do this?
Related
I want to apply data validation to my column so as to only accept whole numbers.
However I want these to be displayed with a dot so as to make it easier to read later on.
e.g. input = 14354 which is valid and then displayed 14.354
the data validation regular expression I am ussing is:
=regexmatch(to_text(A2);"^\d+\.*\d+$")
and the custom formatting is:
#,##
for most this working fine, large numbers are displayed with the '.' and things it shouldnt accept it is rejecting.
However, in the case of numbers which are entered with a decimal point as these are hidden, it is accepting it as valid.
It is also changing the format to auto atic and reading as date such entries like: 15.4
I should point out that I am using sheets in spanish and therefore the , is the marker for decimal places.
What am i missing here??
Select the cell range then go to Data > Data validation...
Add a custom formula rule:
=mod(A1;1)=0
Try this one:
=and(regexmatch(to_text(A2);"^\d+(\.\d{3})*$");mod(A2;1)=0)
Improved your formula to only accept a dot when it is followed by 3 numbers (this way, we invalidate the date e.g A2)
Combining the improved formula of yours and Aresvik's modulo answer, we need to check if the value does not have decimal. (this way, we invalidate the decimal e.g A6)
When both returns true, this shall confirm that the number inputted is a whole number with no decimal and not a date.
Output:
Invalid inputted values:
A2 - 15.4
A6 - 16412,212
So I have an application which holds multiple entries that are strings of comma separated emails. The strings live in text area elements where they can be modified. The application uses JavaScript to modify these strings, I need to use Capybara to watch verify that a target string has the correct number of emails in it. To illustrate what I mean here's my Cucumber (assuming the target list starts with a 5 email string):
When I remove the 3rd email under list one
Then I should see 4 emails under list one
When I click the "Cancel" button for list one
Then I should see 5 emails under list one
I can pretty easily grab the string with Capybara like so:
expect(page).to have_css(".css-selectors textarea")
but I don't know what to do from there. I need to be able to assert that the number of emails in the string is in fact changing to the desired number. I need to split the string and count the number of emails to see if they match the target number, but everything I've tried leads to a race condition where Capybara checks the value before the JS can finish updating. I've looked into passing a filter block to the have_css call but I can't find documentation on how that would work, or if it's even the right tactic. And so I'm out of ideas here.
Since all the emails are in one element your inclination to use a filter block is exactly correct. The filter block receives each element that matches the initial selector and needs to return whether or not it matches whatever extra filtering you wish to do (true/false). Therefore, to check that the element had a string (value not text since it's a form field) with 4 comma separated items it would be something like
expect(page).to have_css(".css-selectors textarea"){ |ta|
ta.value.split(',').size == 4
}
This will then use Capybaras waiting/retrying behavior while also performing the extra step of checking for a matching number of comma separated items in the text of the element, thereby getting around the race condition.
Your check could also be performed by using a regex for the with option of the field selector, along the lines of
expect(page).to have_field(type: 'textarea', with: /^[^,]+,[^,]+,[^,]+,[^,]+$/)
or fillable_field selector
expect(page).to have_selector(:fillable_field, type: 'textarea', with: /^[^,]+,[^,]+,[^,]+,[^,]+$/)
Those don't currently scope to the .css-selectors element but you could do that with within or a chained find. You could also ensure a unique element by passing the id/name/label text of the element. Obviously you could make the regex more complicated if you want to actually verify the text strings are emails, etc.
I need to validate that something is an Excel cell range in Ruby, i.e: "A4:A6". By looking at it, the requirement I am looking for is:
<Alphabetical, Capitalised><Integer>:<Integer><Alphabetical, Capitalised>
I am not sure how to form a RegExp for this.
I would appreciate a small explanation for a solution, as opposed to purely a solution.
A bonus would be to check that the range is restricted to within a row or column. I think this would be out of scope of Regular Expressions though.
I have tried /[A-Z]+[0-9]+:[A-Z]+[0-9]+/ this works but allows extra characters on the ends.
This does not work because it allows extra's to be added on to the beginning or end:
"HELLOAA3:A7".match(/\A[A-Z]+[0-9]+:[A-Z]+[0-9]+\z/) also returns a match, but is more on the right track.
How would I limit the number range to 10000?
How would I limit the number of characters to 3?
This is my solution:
(?:(?:\'?(?:\[(?<wbook>.+)\])?(?<sheet>.+?)\'?!)?(?<colabs>\$)?(?<col>[a-zA-Z]+)(?<rowabs>\$)?(?<row>\d+)(?::(?<col2abs>\$)?(?<col2>[a-zA-Z]+)(?<row2abs>\$)?(?<row2>\d+))?|(?<name>[A-Za-z]+[A-Za-z\d]*))
It includes named ranges, but the R1C1 notation is not supported.
The pattern is written in perl compatible regex dialect (i.e. can also be used with C#), I'm not familiar with Ruby, so I can't tell the difference, but you may want to look here: What is the difference between Regex syntax in Ruby vs Perl?
This will do both: match Excel range and that they must be same row or column. Stub
^([A-Z]+)(\d+):(\1\d+|[A-Z]+\2)$
A4:A6 // ok
A5:B10 // not ok
B5:Z5 // ok
AZ100:B100hello // not ok
The magic here is the back-reference group:
([A-Z]+)(\d+) -- column is in capture group 1, row in group 2
(\1\d+|[A-Z]+\2) -- the first column followed by any number; or
-- the first row preceded by any character
I am doing some localization testing and I have to test for strings in both English and Japaneses. The English string might be 'Waiting time is {0} minutes.' while the Japanese string might be '待ち時間は{0}分です。' where {0} is a number that can change over the course of a test. Both of these strings are coming from there respective property files. How would I be able to check for the presence of the string as well as the number that can change depending on the test that's running.
I should have added the fact that I'm checking these strings on a web page which will display in the relevant language depending on the location of where they are been viewed. And I'm using watir to verify the text.
You can read elsewhere about various theories of the best way to do testing for proper language conversion.
One typical approach is to replace all hard-coded text matches in your code with constants, and then have a file that sets the constants which can be updated based on the language in use. (I've seen that done by wrapping the require of that file in a case statement based on the language being tested. Another approach is an array or hash for each value, enumerated by a variable with a name like 'language', which lets the tests change the language on the fly. So validations would look something like this
b.div(:id => "wait-time-message).text.should == WAIT_TIME_MESSAGE[language]
To match text where part is expected to change but fall within a predictable pattern, use a regular expression. I'd recommend a little reading about regular expressions in ruby, especially using unicode regular expressions in ruby, as well as some experimenting with a tool like Rubular to test regexes
In the case above a regex such as:
/Waiting time is \d+ minutes./ or /待ち時間は\d+分です。/
would match the messages above and expect one or more digits in the middle (note that it would fail if no digits appear, if you want zero or more digits, then you would need a * in place of the +
Don't check for the literal string. Check for some kind of intermediate form that can be used to render the final string.
Sometimes this is done by specifying a message and any placeholder data, like:
[ :waiting_time_in_minutes, 10 ]
Where that would render out as the appropriate localized text.
An alternative is to treat one of the languages as a template, something that's more limited in flexibility but works most of the time. In that case you could use the English version as the string that's returned and use a helper to render it to the final page.
I want a textfield to validated using dreamweaver spry option. I need the input to be in the below mentioned format
05632-25252525
The first (05632) part should contain minimum 3 no's, maximum upto 6.
The second (05632) part should contain minimum 7 no's, maximum upto 10.
how can i write a regular expression or a pattern in Spry. pls suggest
Spry can't handle that as all one text field.
However, if you make it two textfields, then it's simplicity...just set the first one to have min 3 character, max 6 only allow numbers. Set the second to be min7 max10 all numbers and then just combine them after the form is posted.