Regular Expression to validate comma separated numbers using javascript

Today i got situation to validate text field containing comma separated numbers. I know how to validate numbers alone. But don’t know how to validate numbers with comma separated and each number should be 8 digits.

For example, 1234678,54872659,84567892,....

I searched google but didn’t find it and finally i got solution from my friend…

Here is the regular expression to do this trick,

/^(?:\s*\d{8}\s*(?:,|$))+$/

Usage:

<script language="javascript">
var pattern = /^(?:\s*\d{8}\s*(?:,|$))+$/;
var testvalue = document.getElementById('checkNumber').value;
if(pattern.test(testvalue)){
alert("Correct");
} else {
alert("Wrong");
}
</script>

 

More explanations on this regular expression, please check here

http://regex101.com/r/iY9bQ9

Hope this will be useful to you all.

Author: Vinodkumar Saravana.

2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 22,000 views in 2012. If each view were a film, this blog would power 5 Film Festivals

Click here to see the complete report.

Major difference between include and require

You people know very well there are 4 ways to include a file in PHP.

1. include().

2. include_once();

3. require().

4. require_once().

Because of four existence, sometimes beginners gets confuse which right one to use in their code. So in this post, i am going to give you a simple explanation of these four.

include() – This function is used to include a file in the current page and it take single argument. You can specify the file path either relative or absolute path. If the specified path is not found, then it will throw Warning message but still the application continues to run…This is good for production environment.

include_once() - This is similar to include() but only difference is it will include the file only once in the page. For example, you have an loop which contains common code to use in different places, and you put that common code in a separate file. So in this case include_once() will include the code only once even its running in the loop.

require() – This function will include a file in the current page and it take single argument. And this is also similar to include but it will throw fatal error if the specified file path is not found and the application will stops running further.

require_once() - This function will do action of require() and include_once(). So it will include only once and throw fatal error if the file is not found.

In common, there is a big difference between include and require, the include will run the file directly whereas require it will check the entire code in the file and then only it will execute. So require() will be slow when compare to include(). And most developers, will recommend to use require, because it will throw error even if it is there simple or small mistake in your code.

Hope i have explained to you people somewhat clearly….

 

Author: Vinodkumar Saravana

 

WordPress: Disable / Remove forgot password and password reset

One of my clients request me to remove the forgot password and password reset field from the user profile page except admin. I started editing the user-edit.php file to comment the those lines for normal users. But i feel this is not the best way to do it. so i start searching Google and found the below plugin to disable those two options for users.

https://gist.github.com/781573

Just copy the code from the below link and save the file in the plugin folder, don’t forget to close(?>) the php tag which is missing in the above link. Login as admin and activate the plugin, that’s it. After activating this plugin, normal user won’t get the option to update the password or request to change the password.  So in future if you want to enable this for user, just disable this plugin. If i go with my solution then i have to edit the page to enable it again.

Only problem with this code, when admin forget his password himself, he don’t have option to reset it. He have to do manually in phpmyadmin or some other way.

Hope this will be useful for someone who have similar requirement.

Thanks to Derek Herman who contribute this plugin.

Download File in PHP

Today, i have got new issue while downloading document in IE browser. Below is the message i got when i try to download a file in IE from https.

Internet Explorer cannot download file from server. Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be….

I spent more times to figure out the issue and even i changed the settings in IE but no use, still having the same issue. Its working fine in FF and other browsers.

At last i found the issue its because of PHP headers which i used in my code. I searched google and found this code which support in all browsers.


function downloadFile( $fullPath ){

// Must be fresh start
if( headers_sent() )
die(‘Headers Sent’);

// Required for some browsers
if(ini_get(‘zlib.output_compression’))
ini_set(‘zlib.output_compression’, ‘Off’);

// File Exists?
if( file_exists($fullPath) ){

// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);

// Determine Content Type
switch ($ext) {
case “pdf”: $ctype=”application/pdf”; break;
case “exe”: $ctype=”application/octet-stream”; break;
case “zip”: $ctype=”application/zip”; break;
case “doc”: $ctype=”application/msword”; break;
case “xls”: $ctype=”application/vnd.ms-excel”; break;
case “ppt”: $ctype=”application/vnd.ms-powerpoint”; break;
case “gif”: $ctype=”image/gif”; break;
case “png”: $ctype=”image/png”; break;
case “jpeg”:
case “jpg”: $ctype=”image/jpg”; break;
default: $ctype=”application/force-download”;
}

header(“Pragma: public”); // required
header(“Expires: 0″);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);
header(“Cache-Control: private”,false); // required for certain browsers
header(“Content-Type: $ctype”);
header(“Content-Disposition: attachment; filename=\”".basename($fullPath).”\”;” );
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.$fsize);
ob_clean();
flush();
readfile( $fullPath );

} else
die(‘File Not Found’);

}

Hope this will be useful for those having similar problems.

Source: php.net

« Older entries

Follow

Get every new post delivered to your Inbox.