Tuesday 24 January 2023

Password show and hide onClick using vanilla javascript

Hiding passwords visibility in forms helps protect from people looking over your shoulder and reading your password, but greatly increases in the likelihood that someone will enter the wrong one. Today, I want to show you how to implement a simple show/hide password toggle with vanilla JavaScript.

The Form #

Here’s a simple form with a username and password. I’ve also added a checkbox users can click to reveal or hide their password.

<label for="username">Username</label>
<input type="text" name="username" id="username">

<label for="password">Password</label>
<input type="password" name="password" id="password">

<label for="show_password">
	<input type="checkbox" name="show_password" id="show_password">
	Show Password
</label>

How this is going to work

When a user checks the show_password checkbox, we’ll get the password field and change it’s type from password to text. If they uncheck the box, we’ll switch it back to password.

Yea, it’s really that simple!

The JavaScript

Listening for changes

We’ll use addEventListener to listen for click events on our show_password input. This will also detect changes submitted with a keyboard (as in, tabbing onto the checkbox and hitting the enter key).

We’ll listen for all clicks on the document, and check to see if the clicked element was our show_password checkbox—a technique called event delegation.

Whenever a click event happens, we’ll check to see if it has an ID of show_password. If not, we’ll bail.

// Listen for click events
document.addEventListener('click', function (event) {

	// If the clicked element isn't our show password checkbox, bail
	if (event.target.id !== 'show_password') return;

}, false);

Toggling password visibility

Next, we want to get the password field. If no field is found, we’ll bail.

// Listen for click events
document.addEventListener('click', function (event) {

	// If the clicked element isn't our show password checkbox, bail
	if (event.target.id !== 'show_password') return;

	// Get the password field
	var password = document.querySelector('#password');
	if (!password) return;

}, false);

Working Demo Fork Github

How to integrate google Language translator without powered by google using css

 

google translator with css

Today we are going to learn how to integrate google Language translator without showing powered by google using css.

This will help beginners and senior developers to successfully integrate this snippet without stress.

Preview Example

Thursday 7 May 2020

Wednesday 12 February 2020

Preview and upload image using Javascript
Image preview is a great feature for user to check their image before upload whether the correct image is going to upload or if the image looks

If you want to preview a file (image) before it is uploaded. The preview action should be executed all in the browser without using Ajax to upload the image.

The question is "How can I do this?"

if you have been told, "unless you use Gears or another plugin you cannot manipulate the image inside the browser" that's false.

Here's a simple solution that doesn't use jquery, is pure javascript.

There are a couple ways you can do this. The most efficient way would be to use URL.createObjectURL() on the File from your <input>. Pass this URL to img.src to tell the browser to load the provided image.

Here's an example:

Please take a look at the sample code below:

<input type="file" accept="image/*" onchange="loadFile(event)">
<img id="output"/>
<script>
  var loadFile = function(event) {
    var output = document.getElementById('output');
    output.src = URL.createObjectURL(event.target.files[0]);
  };
</script>

You can also use FileReader.readAsDataURL() to parse the file from your <input>. This will create a string in memory containing a base64 representation of the image.

<input type="file" accept="image/*" onchange="loadFile(event)">
<img id="output"/>
<script>
  var loadFile = function(event) {
    var reader = new FileReader();
    reader.onload = function(){
      var output = document.getElementById('output');
      output.src = reader.result;
    };
    reader.readAsDataURL(event.target.files[0]);
  };
</script>

Read carefully: readAsDataURL and createObjectURL will cache an image in the browser, then return a base64 encoded string which references the image location in the cache. If browser cache is cleared, the base64 encoded string will no longer reference the image.

The base64 string isn't actual image data, it's a URL reference to the image, and you shouldn't save it to a database hoping to retrieve image data.
to avoid memory issues you should call URL.revokeObjectURL when you are done with your blob

One-liner solution:

The following code uses object URLs, which is much more efficient than data URL for viewing large images (A data URL is a huge string containing all of the file data, whereas an object URL, is just a short string referencing the file data in-memory):


<img id="blah" alt="your image" width="100" height="100" />

<input type="file" 
    onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">

Generated URL will be like:

blob:http%3A//localhost/7514bc74-65d4-4cf0-a0df-3de016824345

Tuesday 19 November 2019

How to Convert 1,000 to 1k using PHP
Here's a generic way to do this that doesn't require you to use number_format or do string parsing:


function formatWithSuffix($input)
{
    $suffixes = array('', 'k', 'm', 'g', 't');
    $suffixIndex = 0;

    while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes))
    {
        $suffixIndex++;
        $input /= 1000;
    }

    return (
        $input > 0
            // precision of 3 decimal places
            ? floor($input * 1000) / 1000
            : ceil($input * 1000) / 1000
        )
        . $suffixes[$suffixIndex];
}

echo formatWithSuffix(1000);
echo formatWithSuffix(100000); 

Let me know if I missed any of your requirements:












Sunday 18 August 2019

Get Client Machine Name in PHP
This snippet will help you to get the name of your pc


echo gethostbyaddr($_SERVER['REMOTE_ADDR']);

echo gethostname(); // may output e.g,: sandie

// Or, an option that also works before PHP 5.3
echo php_uname('n'); // may output e.g,: sandie

This work locally not server

Server Error Note:
PHP Notice:  Undefined index: REMOTE_ADDR in /home/UMAQ6W/prog.php on line 4
PHP Warning:  gethostbyaddr(): Address is not a valid IPv4 or IPv6 address in /home/UMAQ6W/prog.php on line 4

Friday 16 August 2019

Sum of values from different divs Jquery

   I am loading dynamically divs that have a .totalprice class. At the end, It will sum of the values from all of the .totalprice.










For <div> Elements:

<div class='totalprice'>1.25</div>
 <div class='totalprice'>0.25</div>
 <div class='totalprice'>3.00</div>
 <div class='totalprice'>2.50</div>
 <div class='totalprice'>0.01</div>

 <script>
 var sum = 0.0;
 $('.totalprice').each(function()
 {
     sum += parseFloat($(this).text());
 });
 alert(sum);
 </script>

For <input> Elements (inputs, checkboxes, etc.):
 
Alternatively, if you are looking for an integer, you can use the parseInt() function.
see working demo http://jsfiddle.net/PQ6Lk/ 


Get Prefix From URL in PHP














Get last word from URL after a slash in PHP

Using regex:

preg_match("/[^\/]+$/", "http://www.codexpresslabs.blogspot.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test

Use basename with parse_url:

echo basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

I used this:

$lastWord = substr($url, strrpos($url, '/') + 1);

Thanks to: https://stackoverflow.com/a/1361752/4189000

You can use explode but you need to use / as delimiter:

$segments = explode('/', $_SERVER['REQUEST_URI']);

Note that $_SERVER['REQUEST_URI'] can contain the query string if the current URI has one. In that case you should use parse_url before to only get the path:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

And to take trailing slashes into account, you can use rtrim to remove them before splitting it into its segments using explode. So:


$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$segments = explode('/', rtrim($_SERVER['REQUEST_URI_PATH'], '/'));


Get Current Website Page URL in PHP 8
One reason why you ought not hardcode your URL in config is the point at which you have various stages where your program will be installed on (different server). Every single one of them will have their particular URL, and you would prefer not to change your code as per which server your program is introduced on.


Have a look at $_SERVER['REQUEST_URI'], i.e.

$actual_link = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";

(Note that the double quoted string syntax is perfectly correct)

If you want to support both HTTP and HTTPS, you can use


$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";


Note: using this code has security implications. The client can set HTTP_HOST and REQUEST_URI to any arbitrary value it wants.


Source Stackoverflow.com

Wednesday 3 July 2019

Nairaland Software Script For Everyone - Free Download
Hello guys, now you can download nairaland forum script free without paying a dim

INSTALLATION VIDEO GUIDE



Nairaland PHP Forum Software

Nairaland PHP Forum Software Script is an advanced, robust, best & most powerful free PHP forum software which is stunningly beautiful, extensible and non-developer friendly.
Start boosting your business conversion rates with Nairaland PHP Forum Software Script SEO first development sites with zero downtime

Single platform For Forum & Blog

Nairaland PHP Forum Software Script is open source forum software with a penchant for speed. very flexible hook and module system can satisfy every web master's needs, suitable for Forum discussion and blogging.

  1. Relentless Features
  2. Easy Integration
  3. AI bugs report
  4. Bad Word Filter
  5. Search Engine Optimize
  6. Speed & Robustness

Intuitive Interface

Fully responsive
24/7 Supports
Friendly URL
Simple Admin Dashboard

Even more features

Well Documented
Single Click Update
Clean Codes
Unique Design
Fair price

30 days free Trials 

Download Free Now

After download please  drop your website url for script activation, please your website will be activated within 24hrs, stay calm

Sunday 9 June 2019

How to use %S in PHP, HTML or XML
with printf or sprintf characters preceded by the % sign are placeholders (or tokens). They will be replaced by a variable passed as an argument. 
 
Example:
$str1 = 'best';
$str2 = 'world';

$say = sprintf('Tivie is the %s in the %s!', $str1, $str2);
echo $say;
 
This will output:
Tivie is the best in the world!
Note: There are more placeholders (%s for string, %d for dec number, etc...)

Order:
The order in which you pass the arguments counts. If you switch $str1 with $str2 as
$say = sprintf('Tivie is the %s in the %s!', $str2, $str1); 
it will print
"Tivie is the world in the best!"
You can, however, change the reading order of arguments like this:
$say = sprintf('Tivie is the %2$s in the %1$s!', $str2, $str1);
which will print the sentence correctly.

Also, keep in mind that PHP is a dynamic language and does not require (or support) explicit type definition. That means it juggles variable types as needed. In sprint it means that if you pass a "string" as argument for a number placeholder (%d), that string will be converted to a number (int, float...) which can have strange results. Here's an example:

$onevar = 2;
$anothervar = 'pocket';
$say = sprintf('I have %d chocolate(s) in my %d.', $onevar, $anothervar);
echo $say;
 
this will print
I have 2 chocolate(s) in my 0.
More reading at PHPdocs