Anyone knows these reg exp - what do?

if (preg_match("/\r/i", $tel1) || preg_match("/\n/i", $tel1) || preg_match("/%0a/i", $tel1) || preg_match("/%0d/i", $tel1))

anyone knows these reg exp - what do?

They look for carriage returns or line feeds in the variable $tel1.

1 Like

although I have no idea why they used four separate RegExp for that.

1 Like

Me either. It could have been done in 1 and the 3rd and 4th are unnecessary entirely.

Example: (technically the i at the end is useless too, as line feeds and carriage returns do not have a “case”)

if (preg_match("/(\r|\n)/i", $tel1))
1 Like

if (!preg_match("/^([0-9])+$/", $tel1))

this use to detect if non numberic field?

I would check that with filter_input().

1 Like

why?

it’s easier to apply options and you can submit integer values beyond the scope of digits (scientific notation).

1 Like

phone if exist should be 8 digits so will use strlen…

Phone validation is extremely complex. even if you restrict it only to NANP there are countless possibilities to write a phone number. Go international and you’re effectively screwed.

4 Likes

Regex can include length parameters. But be aware of people adding spaces (they can be stripped out) and using (or not) regional or national code prefixes, depending on the scope of audience.
Personally, I only sanitise phone numbers, not validate. But then I don’t have forms where it’s a mandatory field.
Sometimes, trying to validate something (badly or too strictly) that’s hard to validate, can do more harm than good.
That is not saying you should not attempt to at least sanitise it (everything), and validate what you can.
But many underestimate how complex some validation can be and over simplify it.

2 Likes

Really needed the intro post regexp about if (preg_match(“/(\r|\n)/i”, $tel1)) for a text-box… ?

That make sense to me, users often break lines using enter when typing in text boxes despite the lines wrap anyway. So find and replace those CR and LF with space in submitted text could be useful.

When you say “text box”, do you mean <textarea> or <input type="text">?
I don’t know why you would use textarea for a phone number. I thought a <input type="tel"> would be best.

3 Likes

<input type="text">
this meant

If all you’re interested in doing is removing line breaks, then as SamA74 posted, the tel type may be a better alternative to type text.

tel: HTML5 A control for entering a telephone number. Line-breaks are automatically removed from the input value, but no other syntax is enforced. You can use attributes such as pattern and maxlength to restrict values entered in the control. The :valid and :invalid CSS pseudo-classes are applied as appropriate.

2 Likes

I take a site to refactor and old developer has detect RegEx for line breaks for tel1, tel2 and business name ALL TEXT fields… well really matters detect line breaks and Not submit form? If detect the below is or Not equivalent?

ctype_alnum — Check for alphanumeric character(s)
ctype_alpha — Check for alphabetic character(s)
ctype_digit — Check for numeric character(s)

exist danger if in tel or business name exist line breaks???

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.