I am following this question's solution to validate a Django's TimeField object 06:00:00
in reactjs. The validation fails with the error "Checkin Time" with value "06:00:00" fails to match the required pattern: /^([0-9]{2})\:([0-9]{2})$/ What could be wrong with this regex.
This is my Joi schema. Kindly assist
schema = {
checkin: Joi.string()
.regex(/^([0-9]{2})\:([0-9]{2})$/)
.label("Checkin Time"),
checkout: Joi.string()
.regex(/^([0-9]{2})\:([0-9]{2})$/)
.label("Checkout Time"),
};
The problem is caused by the ^ and $ characters in your regex. They denote the start and end of your string to compare. You basically are saying the value must be exactly two sets of two digit numbers, separated by a colon. However, your example includes seconds (three sets of two digit numbers, separated by colons). In order to support that, while leaving the seconds portion optional, you can do something like this:
/^([0-9]{2}):([0-9]{2})(:[0-9]{2})?$/
We basically create another capture group containing the colon and the seconds portion, and make it optional (? matches 0 or 1 instance of the pattern)
Related
In my angular application, I've a text field to get phone number from user.
User can enter multiple phone numbers
separated by comma (Without country code and omitting 0 of city code)
For example if the phone number is 042-1234567, User will enter 421234567 and multiple numbers will be like
421234567,421234568,...
This regular expression working perfectly fine for me
^((\d{9})(,\d{9})*)$
But there's exception that user can also add extensions with phone number like this
123456786~19, 123456784~46,..
I've tried following Regx and its working fine for these kind of inputs
^((\d{9}~\d{2})(,\d{9}~\d{2})*)$
But I've to use both of these in one single field so I tried to add OR option and made Regx like this
((\d{9}~\d{2})(,\d{9}~\d{2})*)|((\d{9})(,\d{9})*)
But its not working for both kind of inputs. e.g;
Its not validating this 123456789~12,123456789~12,123456789,123456789~12
Any kind of help will be appreciated.
You may use optional groups:
^\d{9}(?:~\d{2})?(?:,\d{9}(?:~\d{2})?)*$
^^^^^^^^^^^ ^^^^^^^^^^^
See the regex demo
Details
^ - start of string
\d{9} - any 9 digits
(?:~\d{2})? - an optional non-capturing group matching
~ - a tilde
\d{2} - any 2 digits
(?:,\d{9}(?:~\d{2})?)* - zero or more sequences of:
, - a comma
\d{9}(?:~\d{2})? - see above
$ - end of string.
Note that if you need the pattern to also match an empty string, just enclose the pattern with an optional non-capturing group, ^(?:\d{9}(?:~\d{2})?(?:,\d{9}(?:~\d{2})?)*)?$.
To check whether input type "time" field is completed (09:00am) I have used a regular expression.
ng-pattern="\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm]))"
But in the same regular expression I want to check whether the input field is empty. For further information, time field can be empty or completed (ex: )(09:30am)
Can anyone help me regarding this..
In an ng-pattern, you need to use
ng-pattern="/^(?:(?:1[0-2]|0?[1-9]):[0-5]\d\s*[AaPp][Mm])?$/"
and if you need to avoid leading/trailing spaces, also add ng-trim="false".
See this regex demo.
The (?:...)? optional non-capturing group is a wrapper for the whole pattern that becomes optional, i.e. can match an empty string.
The ^ anchor will only match at the start of the string, and $ will anchor the match at the end of the string, so that an entire string should match.
In case somebody is looking for a regex for European time format (00:00-23:59), as i was, here is the regex for that:
^(?:1[0-9]|2[0-3]|0?[0-9]):[0-5]\d{1}?$
Hope this helps somebody save a minute or two.
Can we create a mongoose query to return a result of a specific field data without space in between?
for example:
series 'KA 04 A' and 'KA04A' is same. i want to make a check while adding a new series if the new series is already exist in my mongodb thru mongoose.
Currently my series field have space in between
the current code of my mongoose is:
seriesModel.find({series_name:req.body.seriesName.toUpperCase(), status:'active'}, function(err, data){
if(err){
logger.info(err);
return false;
}
else if(data.length){
return res.json({error:true,message:'Series name already exists.'});
}
how can i return field data from db without space. so that i can make a check if the same series exists or not.
Ideally you would standardize your series_name field before saving the record, using a custom hook that strips out whitespace.
That being said, you can use a regular expression in your query. For example, you can search for uppercase/lowercase variations on the word "mongoose" as follows:
model.findOne({name: /mongoose/i}, fn(){...});
One way to solve your problem would be to take your string, split it into individual characters, and then inject the whitespace pattern \s between all the characters (I am assuming there is no leading or trailing whitespace in this field):
"^" + "KA04A".split("").join("\\s*") + "$" // "^K\s*A\s*0\s*4\s*A$"
(The start ^ and end $ are necessary so that we do not get false positives on e.g. "xKA04Ax")
Next, you would need to convert this string into the regex format ( /^K\s*A\s*0\s*4\s*A$/) and plug it into your query:
model.findOne({name: new RegExp("^K\\s*A\\s*0\\s*4\\s*A$")}, fn(){...});
Major Caveat: you will need to be super careful that your original string only contains a-zA-Z0-9 and whitespace. It cannot contain anything that could be mistaken as a regex pattern without first escaping those characters (see here for more on this approach).
I am trying to create a formula field that checks a string that is a series of concatenated values separated by a comma. I want to check the first two characters of each comma separated value in the string. For example, the string pattern could be: abcd,efgh,ijkl,mnop,qrst,uvwx
In my formula I'd like to check if the first two characters are 'ab','ef'
If so, I would return true, else false.
Thanks.
To do this properly, you need to use a regular expression. Unfortunately the REGEX function is not available in formula fields. It is, however, available in formulas in Validation Rules and in Workflow Rules. You can, therefore, specify the below formula in either of a Validation or Workflow Rule:
OR(
AND(
NOT(
BEGINS( KXENDev__Languages__c, "ab" )
),
NOT(
BEGINS( KXENDev__Languages__c, "ef" )
)
),
REGEX( KXENDev__Languages__c , ".*,(?!ab|ef).*")
)
If it's a Validation Rule, you're done -- this formula will create an error if any of the entries do not start with "ab" or "ef". If it's a Workflow Rule, then you can add a Field Update to it to update some field with False when this formula is true (if this formula is true then there is at least one item that doesn't start with ab or ef, so that would make your field False).
Some may ask "What's with the BEGINS statements? Couldn't you have done this all with one REGEX?" Yes, I probably could, but that makes for an increasingly complex REGEX statement, and these are quite difficult to debug in Salesforce.com, so I prefer to keep my REGEXes in Salesforce.com as simple as possible.
I suggest you to search for ',ab' and ',ef' using CONTAINS method. But first of all you need to re implement method which composes this string so it puts ',' before first substring. At the end returned string should look like ',abcd,efgh,ijkl,mnop,qrst,uvwx'.
If you are not able to re implement method which compose this string use LEFT([our string goes here],2) method to check first two chars.
I have a delphi application that uses tdbf which is based on tdataset with the advantage of not requiring bde engine. I needed the table to be sorted and I did this one a single field by adding an indexdef and then specifying the indexfieldnames.
I am trying to now get it to sort on 2 fields ie group men together and then women together and then have each group sorted on salary so that we see females from lowest earner to highest earner followed by men in the same way.
I have read every piece of material stating that you simply specify sortfield of the indexdef as 'gender+salary'. When I try use the index I get told that '+' is not a valid fieldname. I have tried every delimeter from '.'. ','. '&' and ';'. Every delimeter gets picked up as a field that doesn't exist. What is the correct way to sort a table on multiple fields?
Thanks in advance
Clinton Brits
xBASE (dBASE and it's derivatives) requires that fields in an index all be converted to the same data type, typically strings. To do that typically requires some common functions:
DTOS() - Converts an xBASE date to the format CCYYMMDD as a string
STR() - Converts a numeric to a string, with an optional width specifier (default 10) and number of digits to the right of the decimal point. Specifically, the syntax is specified as STR(<numeric> [, <width> [, <decimaldigits>] ]).
SUBSTR() - Extracts a portion of a string from another, with a specified starting position and number of characters
IIF() - Immediate IF, used to convert logicals (eg., IIF(Married = .T., 'Y', 'N')
Index expressions are indeed combined with the + operator. The error you're receiving is probably because you haven't converted to a common data type.
As you've specified the Gender column (probably defined as CHAR 1) and Salary column (probably a NUMERIC of some size), you can use something like
Dbf1.AddIndex('GENDER_SAL', 'GENDER + STR(SALARY, 10, 0)', []);
This creates a index on an expression like F 10000, F 200000, M 12000, where SALARY is converted to the default width of 10 characters (left padded with spaces) and no decimal digits. This should work for you.
I have not used the component, but it looks like they want to use index expressions that are similar to what we used to use in dBase III. On page 7 in the PDF version of the documentation, they offer an example under the Expressions topic:
Dbf1. AddIndex('INDEX1 ', 'DTOS( DATEFIELD)+ SUBSTR ( LONGFIELD ,1 ,10)+ SUBSTR
( LONGFIELD2 ,1 ,20)', []);
You could try their SubStr function on your fields with parameters that would include the whole string and see if that at least gets you a result.