Harsh
Harsh

Reputation: 2096

simple . validation for php

. validation for php

Actualy i want to validate with . like when user enter abc.dfd then true otherwise false in abc only !!

Some other examples. All are true:

adfasf.fdasf
asf.fasdf
adfds.afds
adfs.afdsf

while these are false:

fsd
fsd
fsd

Upvotes: 0

Views: 298

Answers (2)

John
John

Reputation: 7910

you can use this function:

 function validate($str)
 {
   if(preg_match('/[A-Za-z0-9]{1,}\.[A-Za-z0-9]{1,}/', $str))
    {
      return true;
    }
    else
    {
      return false;
    }
  }

how to use:

 if(validate("abc.acc"))
 { 
  //valid string, do something
 }
 else
 {
   //invalid string
 }

or

 $string = "acb.ccs.ghd";
 if(validate($string))
 { 
  //valid string, do something
 }
 else
 {
   //invalid string
 }

returns:

$string = "asgasgasgasg"; // false
$string = "asgasga.sgasg"; // true
$string = "asgasga.sgasg.asgasg"; // true
$string = "asgasga."; // false

Upvotes: 1

Dunhamzzz
Dunhamzzz

Reputation: 14808

As the comment suggested, to check for a period or any particular string inside a string, just use strpos() and do a sstrict check (!==) check for false.

if(strpos($input, '.') !== false) {
    // There is a period
} else {
    // No Period
}

Upvotes: 1

Related Questions