Page 3 of 3

Regular expression for at least one number capital letter and a special character


Here we are going to write a regular exprestion for Password to check the condition like , password must contain at least one number,one capital letter and one special character.

First we can write Regex as below,

^(((.*\d.*[A-Z].*[!@#$%^&*? ~].*))|(.*[A-Z].*\d.*[!@#$%^&*? ~].*)|(.*[!@#$%^&*? ~].*[A-Z].*\d.*)|(.*[!@#$%^&*? ~].*\d.*[A-Z].*))$

This will check the string contain at at least one number,one capital letter and one special character,

Here i am going to show examples with Javascript function ,

We can write a function in javascript as below

 function validatePassword(str)
            {
                // patter to match : Atleast one number ,one capital letter, one special charector
                var reg = /^(((.*\d.*[A-Z].*[!@#$%^&*? ~].*))|(.*[A-Z].*\d.*[!@#$%^&*? ~].*)|(.*[!@#$%^&*? ~].*[A-Z].*\d.*)|(.*[!@#$%^&*? ~].*\d.*[A-Z].*))$/;
                if (reg.test(str)) {
                    return true;                                     
                }
                else {
                    return false;                  
                }
            }

and we can pass the string as function argument “str”, It will return if it matches the condition else it will return false.

Let us look on the demo

Javascript function sample

Enter password :


Let us look a jquery demo

Jquery sample

Enter password :


You can download from github

Have a noce day 😉

 12,293 total views

How to add integer only in a text field using jquery


To allow integer only in text field we can use the following jquery function .
We can write below code inside a document.ready function and give the class “integer-allowed” for the text box we needed.


 $('.integer-allowed').keyup(function () {
                            this.value = this.value.replace(/[^0-9\.]/g, '');
                            }); 

Working demo


Enter value (Numbers only allowed)

Working code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 <script  type="text/javascript">
$(document).ready(function() {
   $('.integer-allowed').keyup(function () {
          this.value = this.value.replace(/[^0-9\.]/g, '');
       });
});
</script>


<strong>Enter value (Numbers only allowed)</strong><br />
<input type="text" class="integer-allowed" name="demoname" /><br />
<input type="submit" class="" name="demosubmit" />

 2,817 total views,  1 views today

Ignore files from git without .gitingore

We can remove tracking of git for some file using below comment .

git update-index --assume-unchanged <file full path>

To track the file again we can use below comment

git update-index --no-assume-unchanged <file  full path>

Now let us look How we can list all the files who is included in no-assume

We can type below comment in terminal

git ls-files -v

It will list like

H file 1
H file 2
h file 3
h file 4

Here we can see first character as capital or lower for some file.

If the first character is lowercase, it is marked as “assume unchanged”, else it is marked as “assume changed”

We can simply list all the “assume unchanged”files using below comment.

git ls-files -v | egrep -r "^h .*"

it will list only “assume unchanged” file list

Example

h file 3
h file 4

 5,531 total views

How to use crone jobs from Cpanel


Hello gays,

Here i am writing how we can runcrone job on cpanel.

1. First go to cpanl->cron job tab.

cronjob1

2. Then we can see a screen like below .

cronjob2

3. We can set the time either from Common Settings or set each and every fields as we want.

4. Finaly we want to write command to run as below.

/usr/bin/curl --user-agent cPanel-Cronn http://domainname.com/page.php/parameters

That is it
😉

 2,592 total views

Force download file php


Hi,

Today we are going to learn about force downloading prompt in PHP.

While i am doing my projects, i had a requirement for file download, File type will be changed, So i made a research on this and made simple useful script which can be use any where to prompt download by giving path and file name . Let us create a function download_file and pass full file path and filename , we can use third argument to mention the MIME Type of file

download_file($sourceFile, $fileName, $mime_type = '') 

Next we can check whether we have permission to read the file.if not it will return 404 error

 if (!is_readable($sourceFile)) {
        header("HTTP/1.1 404 Not Found");
        return;
    }

Now we are going to create an array of list of mime types. so we can compare with this array and set correct header content type

 $mime_types_list = array(
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg" => "image/jpg",
        "jpg" => "image/jpg",
        "pdf" => "application/pdf",
        "csv" => "application/csv",
        "txt" => "text/plain",
        "html" => "text/html",
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "php" => "text/plain"
    );

Next we are going to set mime type if we already pass the MIME type into function it will take that MIME ,else we are going to use a function pathinfo to retrieve the extension of file and we will compare with already created array to get content type

 $path_parts = pathinfo($sourceFile); //pathinfo 



    if ($mime_type == '') {

        $file_extension = $path_parts['extension'];
        if (array_key_exists($file_extension, $mime_types_list)) {
            $mime_type = $mime_types_list[$file_extension];
        } else {
            $mime_type = "application/force-download";
        };
    };

After that we will clean and start the output buffer

  @ob_end_clean();

Then we can set the content type and headers

if (ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');

    header('Content-Type: ' . $mime_type);
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    header("Cache-control: private");
    header('Pragma: private');

lastly we are going to read each line by line and print that as shown below.

$chunksize = 1 * (1024 * 1024);
    $bytes_send = 0;
    if ($sourceFile = fopen($sourceFile, 'r')) {


        while (!feof($sourceFile) &&
        (!connection_aborted())
        ) {
            $buffer = fread($sourceFile, $chunksize);
            print($buffer); // is also possible
            flush();
        }
        fclose($sourceFile);
    } else {
        header("HTTP/1.1 505 Internal server error");
        return;
    }

That is it !!!!! Now we can have a look, How we can read the file using created function We can make a path file and the name we want to give while downloading and pass to function . It will download prompt for file. example for reading file and download prompt

$filepath = 'sample.pdf';
$filename = 'sample.pdf';

download_file($filepath, $filename);
exit;
?>

Full code

<?php

/**
 * function to download a file from server
 * 
 * list all the values in grid
 * 
 * @auther Shabeeb  <me@blog.shabeebk.com >
 * @createdon   : 06-08-2014
 * @
 * 
 * @param source $sourceFile : file pathe 
 * @param source $fileName : file name for download 
 * @param source $mime_type : MIME type optional field
 * @return type
 * exceptions
 * 
 * 
 */
function download_file($sourceFile, $fileName, $mime_type = '') {

    if (!is_readable($sourceFile)) {
        header("HTTP/1.1 404 Not Found");
        return;
    }

    $size = filesize($sourceFile);
    $fileName = rawurldecode($fileName);


    $mime_types_list = array(
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg" => "image/jpg",
        "jpg" => "image/jpg",
        "pdf" => "application/pdf",
        "csv" => "application/csv",
        "txt" => "text/plain",
        "html" => "text/html",
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "php" => "text/plain"
    );

    $path_parts = pathinfo($sourceFile); //pathinfo 



    if ($mime_type == '') {

        $file_extension = $path_parts['extension'];
        if (array_key_exists($file_extension, $mime_types_list)) {
            $mime_type = $mime_types_list[$file_extension];
        } else {
            $mime_type = "application/force-download";
        };
    };

    @ob_end_clean();

    // if IE, otherwise Content-Disposition ignored
    if (ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');

    header('Content-Type: ' . $mime_type);
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    header("Cache-control: private");
    header('Pragma: private');


    $chunksize = 1 * (1024 * 1024);
    $bytes_send = 0;
    if ($sourceFile = fopen($sourceFile, 'r')) {


        while (!feof($sourceFile) &&
        (!connection_aborted())
        ) {
            $buffer = fread($sourceFile, $chunksize);
            print($buffer); // is also possible
            flush();
        }
        fclose($sourceFile);
    } else {
        header("HTTP/1.1 505 Internal server error");
        return;
    }

    die();
}

$filepath = 'sample.pdf';
$filename = 'sample.pdf';

download_file($filepath, $filename);
exit;
?>

Download the source

 4,707 total views

Hello world in angular


Guys,

Today we are going to learning Hello world in Angularjs .

AngularJS, a JavaScript M-V-W (Model-View-What ever) framework developed by a Googler and supported by Google.
if you are using jquery in your application , Angular will amaze you. We are writing very less code in jquery , angular will minify it again !!

Angularjs is giving amazing support to AJAX and we can make a very interactive website with angularjs.

Angular can be defined as a client side javascript framework adding intractivity to HTML. As i mention on top angular is MVW framework
which means ,model view and what ever. There are many software architecture patterns like MVC,MVP (Model View-presenter).

But angular doesn’t care about software architecture . A basic concept of MVW is that all definitions are associated with a named Module.
Angular come with a rich set of APIs to defined and this modules can be linked together by dependency injection.

Hello world in Angular js

let us try a simple hello world in angular js

in order to write angular js in our application we need to include angular js in our HTML page,
We can download and include or we can directily include from CDN

Here i am including


<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.16/angular.min.js"></script>
<div  ng-app>  
  Write some text in textbox:
    <input type="text" ng-model="text" />
 
    <h1>Hello {{ text}}</h1>
</div>

Note that we didn’t write a single line of JavaScript and still this example works like a charm! Live example

Write some text in textbox:

Hello {{ text}}

Let us check step by step

Here i had included “ng-app” after. This tell angular that , angular need to monitor this section if we want angular to monitor one part of our application we can wrap with a div and we can write “ng-app” there. So angular will only monitor that part.

Now we defined ng-model=”text” in text box ng-model bind the state with model value.if we made any changes in the
textbox it will reflect {{text}}.

This is twoway binding. Angular will always keep an eye in ng-model defined and when ever it is changing ,
it will reflect on the ng-model value in double curly braces. .

This is a simple hello world application in angularjs

 2,361 total views

Simple form submit in angularjs with php


Guys,

Today we are going to learning Angularjs, just try out What I found, an easier and simple way to write simple form submit in angularjs with php.

angularjs_PHP_form_submission

angular js with php form submission

We can make a HTML form to submit and save to database,

Here we are going to include 2 js files; one is Angularjs library and another one formjs.js which we will be using in writing controller of our form.

Let us dig deep into the form

  • We will be adding ng-app=”formExample” after the html tag -ng-app tells the angular, this block needs to be watched by the angular module.
  • After that we need to add the controller to form tag like ng-controller=”formCtrl”
    – we are going to define formCtrl in formjs.js to tell what are the action need to do with that form
  • Lastly we will add ng-click=”formsubmit(userForm.$valid)”
    On clicking the button, it will go to formsubmit function and we will pass the validation rules as parameters. We will use native $valid function to validate the form.

Sample HTML page – index.html page

<!DOCTYPE html>
<html ng-app="formExample">
    <head>
        <title>simple form with AngualrJS</title>
        <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
        <script src="js/angular.js" ></script>
        <script src="js/formjs.js"></script>
    </head> 

    <body>

        <div ng-controller="formCtrl">
            <form  name="userForm"  class="well form-search"   >
                
                <input type="text" ng-model="name" class="input-medium search-query" placeholder="Name" required >
                <input type="email" ng-model="email" class="input-medium search-query" placeholder="Email" required >
                <input type="text" ng-model="message" class="input-medium search-query" placeholder="Message" required >
                <button type="submit" class="btn" ng-click="formsubmit(userForm.$valid)"  ng-disabled="userForm.$invalid">Submit </button>
		
            </form>
            <pre ng-model="result">
                {{result}}
            </pre>
        </div>
    </body>

</html>

Now we will have a look on the javascript (formjs.js) file

  • First we are going to define the module as “formExample”.(We have already included in html as mentioned above).
  • Next to that we will define our controller with two dependency, ‘$scope’ and ‘$http’. $scope is for current scope and $http for ajax submission.
  • Later we will define formsubmit function and we will pass the validation status as parameter.
  • Now we will fetch all the values from the form and validate, on successful validation it is send to submit.php as json data. If form validation fails, it will show an alert message.

javascript file
formjs.js

<pre class="lang:default decode:true " title="formjs.js" >


/**
 * @filesource : formjs.js
 * @author : Shabeeb  <mail@shabeebk.com>
 * @abstract : controller fo HTML page
 * @package sample file 
 * @copyright (c) 2014, Shabeeb
 * shabeebk.com/blog
 * 
 *  */



var app = angular.module('formExample', []);


app.controller("formCtrl", ['$scope', '$http', function($scope, $http) {
        $scope.url = 'submit.php';

        $scope.formsubmit = function(isValid) {


            if (isValid) {
              

                $http.post($scope.url, {"name": $scope.name, "email": $scope.email, "message": $scope.message}).
                        success(function(data, status) {
                            console.log(data);
                            $scope.status = status;
                            $scope.data = data;
                            $scope.result = data; 
                        })
            }else{
                
                  alert('Form is not valid');
            }

        }

    }]);</pre> 

In submit.php page we are going to retrieve the json data and decode using it json_decode.
For that we can use the below code:

$post_date = file_get_contents(“php://input”);
$data = json_decode($post_date);

Below is the query to save data to database .
Here I am just printing all the values which we will get from the form for illustration.
php submission form

submit.php

<?php
/**
 * @filesource : submit.php
 * @author : Shabeeb  <mail@shabeeb.com>
 * @abstract : simple submission php form
 * @package sample file 
 * @copyright (c) 2014, Shabeeb
 * 
 * 
 *  */

$post_date = file_get_contents("php://input");
$data = json_decode($post_date);


//saving to database
//save query

//now i am just printing the values
echo "Name : ".$data->name."n";
echo "Email : ".$data->email."n";
echo "Message : ".$data->message."n";



?>

Download files

Demo

Enjoy your Coding with Coffee 😉

 19,614 total views

Initiate git on server

How to initiate git through ssh

step 1 : login into ssh

step 2 : create a folder using mkdir commend and change to that folder

step 3 : write below comment to initate as a master branch

git init –bare

step 4 : change window.pack to 0

git config pack.window 0

step 5 : clone to local machine using

git clone username@host.com:/home/folder name(if needed)

step 5 : make changes

create a read me file and commit

git add .

git commit -am “intial commit ”

step 6 : push server

git push origin master

(before that make sure you take a pull from server git pull origin master)

 

step 7 :

To auto update in server

go to git_folder/hooks/post-update.sample and rename to post-update

and the past this code (change the folder name to targeted folder)

cd ~/public_html/folder|| exit
unset GIT_DIR
git pull origin master
exec git-update-server-info

 

enjoy coding

 0 total views