Topic: ¿REG EX?

Hello community, I would like to validate some data (Email, DNI, date format) before saving them in the database. For this, do you know regular expressions but do not know how to use them, could you put some examples?

Re: ¿REG EX?

Hi, any suggestion from the community?

Re: ¿REG EX?

I explain the situation.  In a text field whose maximum length is 11 a string of type text is inserted.  What is inserted corresponds to the identification number of the person or the number of her passport.  The passport number has 7 digits and it is not necessary to validate it for now, but the identification number of the person if it maintains a pattern, of the 11 digits the first 6 are the date of birth (yymmdd) of that person and the last digit  indicates sex, even for the feminine and odd for the masculine.  What I want to validate is that in the OnChange event of the text box if the number has 11 digits that the first 6 always correspond to a valid date data.

procedure frm_add_estudiante_ed_CI_OnChange (Sender: TObject);
var
v_largo_string:integer;
v_string,v_substring,v_substringdd,v_substringmm,v_substringyy:string;
v_date:TDateTime;
begin
v_largo_string := Length(frm_add_estudiante.ed_CI.Text);

If v_largo_string = 11 Then
begin
v_string := frm_add_estudiante.ed_CI.Text;
v_substringdd := Copy(v_string,5,2);
v_substringmm := Copy(v_string,3,2);
v_substringyy := Copy(v_string,1,2);
v_substring := v_substringdd + '/' + v_substringmm + '/' + v_substringyy;
v_date := StrToDate(v_substring); // The error is here when the data does not have a valid date format.

if FormatDateTime('yyyy',v_date) > FormatDateTime('yyyy',Now) then
begin
v_substring := v_substringdd + '/' + v_substringmm + '/' + v_substringyy;
v_date := IncMonth(StrToDate(v_substring),-1200);
frm_add_estudiante.DateTimePicker_FNacimiento.DateTime := v_date;
End Else
begin
v_substring := v_substringdd + '/' + v_substringmm + '/' + v_substringyy;
v_date := StrToDate(v_substring);
frm_add_estudiante.DateTimePicker_FNacimiento.DateTime := v_date;
end;

End Else
begin
frm_add_estudiante.DateTimePicker_FNacimiento.Checked := False;
end;
end;

Re: ¿REG EX?

This is not a regex but do the trick. I hope you can get some idea with this one.

Post's attachments

Attachment icon wenchester21.zip 497.11 kb, 245 downloads since 2021-02-25 

brian

5 (edited by wenchester21 2021-03-01 13:29:26)

Re: ¿REG EX?

Hello, any idea how to make this work?

procedure frm_add_estudiante_ed_CI_OnChange (Sender: TObject);
var
RegularExpression : TRegEx;
Match : TMatch;

begin
  RegularExpression.Create('[-+]?[0-9]*\.?[0-9]+');
  Match := RegularExpression.Match(ed_CI.Text);
  if Match.Success then
   begin
    ShowMessage('Success.');
   end else
    begin
     ShowMessage('Not successful.');    
    end;
end;

6 (edited by wenchester21 2021-03-02 00:17:06)

Re: ¿REG EX?

brian.zaballa wrote:

This is not a regex but do the trick. I hope you can get some idea with this one.

Hello brother, the solution you gave me was useful, I only modified the part of the date condition for this.  Thanks a lot.

if yy <= StrToInt(Copy(DateToStr(date),9,2)) then 
yy := (StrToInt(Copy(DateToStr(date),7,2))*100)+yy 
else yy := ((StrToInt(Copy(DateToStr(date),7,2))-1)*100)+yy;

Thanks a lot.

Re: ¿REG EX?

You're welcome. Been trying to make your regex code to work but no luck is with me. Mind to explain what are you trying to obtain with that regex? I'm not that good when it comes to regex but maybe I can or someone can help you with the output you are trying to obtain, but not doing the regexp class. I can't find any documentation to make it work.

brian

Re: ¿REG EX?

I want to use the regular expression to validate that the date type data is correct.  The expression that I put in the example does not correspond to a date but to a decimal number.  But that's the least of it, by putting the correct regular expression you can already validate the date or whatever you want.  This route is the one that I would see as the most suitable but the one you showed me is also quite good.  It works for me to show the messages, they are to know if it validates correctly, the actions I put them later.

Re: ¿REG EX?

Regular expression for dates

^(((0[1-9]|1[012])/(0[1-9]|1\d|2[0-8])|(0[13456789]|1[012])/(29|30)|(0[13578]|1[02])/31)/[2-9]\d{3}|02/29/(([2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

10 (edited by brian.zaballa 2021-03-02 13:53:00)

Re: ¿REG EX?

wenchester21 wrote:

Regular expression for dates

^(((0[1-9]|1[012])/(0[1-9]|1\d|2[0-8])|(0[13456789]|1[012])/(29|30)|(0[13578]|1[02])/31)/[2-9]\d{3}|02/29/(([2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

Woa, that's a complex one. I really not into regex. LMAO. Anyway I think it is because of the leap year, and other things to check on date. I hope MVD Dev will update or create a sample for that TRegexp. I just don't know if that class TRegexp is derived from TRegexpr which I find workable. I'm just a newbie when it comes to pascal delphi. smile

brian