Posts Tagged regular_Xpression

Using regular expression with PHP

Before use a regular expression, you have to create your own regular expression depending on your projectes requirments.  lets think about password. What you do, if you have password restriction like: password must have letter and number. And there must be 1 letter and 1 number.

  • Create my regular expression with rules:
    • ‘(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,12})$’;
  • Create object to use this rules:
    • var regObjPass = new RegExp();
  • Store this rules in a variable:
    • var passRegExp = ‘(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,12})$’;
  • Use this rules for compile:
    • regObjPass.compile(passRegExp);
  • Take the field’s value and store it in a variable:
    var agentPass =  frmName.agentPass.value
  • Now, apply the condition and check the field for validation
    • if(agentPass==” ){
      errorMsg=errorMsg+”\n# Password field can’t be blank”;
      flag= false;
      }
      else if(!regObjPass.test(agentPass) ){
      errorMsg=errorMsg+ “\n# Password must have 1 digit & 1 character.\n Password can’t contain any special character.\n Password length must be 6 to 12.” ;
      flag= false;
      }

Add comment July 14, 2009

Write Your own Regular expression

In everyday work,we need to valid fields with regular expression. In hurry, we do google and got confused with so many options of same type validation. If we know the rules, than it will very easy to make our own expression.

We can make expression for different,but common cases :

  1. only character set:  first name, last name, paragraph title.
    • Name: ‘^[ \t\r\n]?[ \t\r\na-zA-Z._]+[ \t\r\n]?$’;
    • Paragraph title: ‘^[ \t\r\n]?[ \ta-zA-Z]{10,80}[ \t\r\n]?$’;
  2. only number set:  postcode, phone number, user id
    • Phone number: ‘^[ \t\r\n]?[0-9]{8,16}[ \t\r\n]?$’
    • Only Number:  ‘^[ \t\r\n]?[0-9]{1,10}[ \t\r\n]?$’
    • Post code: ‘^[ \t\r\n]?[0-9]{4,6}[ \t\r\n]?$’;
  3. only number and character set: Address,password
    • Address: ‘^[ \t\r\n]?[ \t\n\/a-zA-Z0-9._%,-]*[ \t\r\n]?$’
    • Password : ‘(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,12})$’ [with at least 1 character and 1 number]
  4. character with some special character :email address
    • Email Address: ‘^([ \t\n\r\f\v]?)([a-z]{1})[a-z0-9._%-]+@[a-z0-9.-]+[.]([a-z]{2,4})([ \t\n\r\f\v]?)$’

Now, whan to do little discussion about this rules. I hope it will help to understand the meaning of signs. lets take the last expression,which one is created for Email. I have added color, to make some part of the expression.

RULES:^([ \t\n\r\f\v]?)([a-z]{1})[a-z0-9._%-]+@[a-z0-9.-]+[.]([a-z]{2,4})([ \t\n\r\f\v]?)$’

([ \t\n\r\f\v]?) ->with this ‘?’ it indicate that,there can be one or Zero \t (tab), \n (new line)

([a-z]{1}) ->with this ‘{1}’ indicate that,there must be 1 character any one of a to z.

[a-z0-9._%-] ->with this rules it defines that,there can be any one of [ a to z ] or [ 0 to 9] or [.(DOT)] or [-] or[ _ ].

+@ –>after those characters there must be a @ sign

[a-z0-9.-] ->with this rules it defines that,there can be any one of [ a to z ] or [ 0 to 9] or [.(DOT)] or [-] .

+[.] -> must be a DOT(.)

([a-z]{2,4}) -> should have character. and the length should be 2 to 4 character.

Example: hee9.llo_wor-ld@g8.com

Add comment July 14, 2009

Field validation with php

For creating a php file, at first we have to check all the fields input are valid. so, Field validation is our 1st step:

if(submit is ok){

if ( ! preg_match( “/^( [ tnrfv]* )+ ( [ a-zA-z_ ] ){ 4 , 20 }+/” , $fastname ) ){

$validation_flag=0;
$error_counter++;
}
else{
$validation_flag=1;
}

if ( ! preg_match( “/^( [ tnrfv]*)+( [ a-zA-Z0-9_- ]){ 4,20 }/”, $password ) ){
$validation_flag=0;
}else
$validation_flag=1;
if (!preg_match( “/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)*.([a-zA-Z]{2,6})$/”, $email_address ) )   {
$validation_flag=0;
$error_counter++;
}else{
$validation_flag=1;
}

}

Add comment December 14, 2008

Previous Posts


My Profile


# Software Eng. # Worked with C,C++, JAVA and PHP. #Love to play with codes. # Mostly like to do programming with 'C'. # I am not a CODER, but a DEVELOPER.

Admin Panel..

Life Cycle

December 2009
S S M T W T F
« Sep    
 1234
567891011
12131415161718
19202122232425
262728293031  

Clouds

cakephp codeIgniter desktop htaccess jscript linux mixed my words php regular expression

Just Posted

TOP R@TED

TaGs

cakephp codeIgniter desktop GP htaccess image upload installation jscript linux news password Personal Life php regular_Xpression script session validation

Archives

Top Posts

CoMmEnTs

Blog Stats

My Web Links

Web Links

RSS Xperts.PHP