/    Sign up×
Community /Pin to ProfileBookmark

Validate String (Regular Expression)

I working on the form where i want to restrict the user to input only characters,digit, . , and white space. i want to validate 2 text field and one text area.

[code=html]if(!preg_match(“/^[a-zA-Z0-9].*$/”,$field1))
{
$turnmsg = “<b><font color=’red’> Only letters,spaces & . are allowed </font></b>”;
}

if(!preg_match(“/^[a-z A-Z].*$/”,$field2))
{
$turnmsg = “<b><font color=’red’> Only letters,spaces & . are allowed </font></b>”;
}

if(!preg_match(“/^[a-z A-Z].*$/”,$textarea1))
{
$turnmsg = “<b><font color=’red’> Only letters,spaces & . are allowed </font></b>”;
}[/code]

The problem is that when i applied preg_match on single field with if condition its working fine , but with the mutiple if condition its fail. any alternatives or do i needs any modificaiton into my code.

Thanks ,

Ashish

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bionoidNov 03.2013 — Modified your expression a little, and seeing that you require it on all three fields I'm appending them together:

$turnmsg = '';

if (!preg_match('[COLOR="#FF0000"]/^[a-zA-Zds.]*$/[/COLOR]', $field1 . $field2 . $textarea1))
{
$turnmsg = "&lt;b&gt;&lt;font color='red'&gt; Only letters,spaces &amp; . are allowed &lt;/font&gt;&lt;/b&gt;";
}

echo $turnmsg;
Copy linkTweet thisAlerts:
@Ashish_SoodauthorNov 04.2013 — Its not working still i can able to enter special characters into my database(%,$#@ etc)
Copy linkTweet thisAlerts:
@bionoidNov 04.2013 — Not that I could really see anything wrong with the previous expression, I've changed it a little again anyway.

Also how do you determine a validation error, or if you need to write to the database?

&lt;?php

$field1 = 'abc.123 ' . "nrt";
$field2 = '';
$textarea1 = '%$#@';

$turnmsg = '';

if (preg_match('[COLOR="#FF0000"]/[^a-zA-Zds.]/[/COLOR]', $field1 . $field2 . $textarea1)) {
$turnmsg = "&lt;b&gt;&lt;font color='red'&gt; Only letters,spaces &amp; . are allowed &lt;/font&gt;&lt;/b&gt;";
} else {

<i> </i>[COLOR="#FF0000"]//WRITE TO DATABASE?[/COLOR]

}

echo $turnmsg;

?&gt;


The current example outputs:

[COLOR="#FF0000"][B]Only letters,spaces &amp; . are allowed[/B][/COLOR]
Copy linkTweet thisAlerts:
@Ashish_SoodauthorNov 04.2013 — Thanks for your reply, its working for me now.
Copy linkTweet thisAlerts:
@Ashish_SoodauthorNov 04.2013 — @ bionoid

One more question can i use [B]htmlspecialchars [/B]or [B]htmlentities [/B]so i can avoid my form from hackers

if yes then could you please give me an example by taking my above code

Thanks again
Copy linkTweet thisAlerts:
@Ashish_SoodauthorNov 04.2013 — Is this right way to used htmlspecialchars function

[B]$field1= htmlspecialchars($_POST['field1']);[/B] ?
×

Success!

Help @Ashish_Sood spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...