Blog

Client-side form validation using the jQuery Validate plugin

This article lets us understand how to validate a form using the jQuery Validate plugin.

Step 1: Create an HTML form

<html>
<head>
        <meta charset="utf-8">
        <title>Register</title>
        
		<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.5/jquery.validate.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
		
		<script type="text/javascript" src="task.js"></script>
        <link rel="stylesheet" href="task.css">
    </head>

<body>    
    <div class="row" background-color=#FFFFFF>
	<div class="column">
	<div class="container">
	<img src="imag1.png" align="center" alt="LoginForm"">
	</div>
	</div>
	<div class="column">
	<div class="container">
	<h1> SignUp </h1>    
    <form name="signup" id="signup">  
        <div class="container">   
            <label>Username : </label>   
            <input type="text" placeholder="Enter Username" name="username" required>  
			<label>Email: </label> 
			<input type="text" name="email" required="required" id="useremail">
            <label>Password : </label>   
            <input type="password" placeholder="Enter Password" name="password" required>  
			<label>Confirm Password : </label>   
            <input type="password" placeholder="Enter Password again" name="confirm_password" required> 
            <button type="submit">SignUp</button>   
        </div>   
    </form> 
</div>	

</div>
</div>	
</body>     
</html> 

Step 2: Add CSS to improve the appearance

Body {  
  font-family: Calibri, Helvetica, sans-serif;  
  background-color: white;  
}  
.column {
  float: left;
  width: 50%;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
.img{
	height:100%;
	width:100%;
	object-fit:contain;
}
.p{
	display:flex;
	flex-direction:row;
}
p:before, p:after{
	content:"";
	flex:1 1;
	border-bottom:2 px solid #000;
	margin:auto;
}

	
button {   
       background-color: #4CAF50;   
       width: 100%;  
        color: orange;   
        padding: 15px;   
        margin: 10px 0px;   
        border: none;   
        cursor: pointer;   
         }   
 form {   
        border: 3px solid #f1f1f1;   
    }   
 input[type=text], input[type=password] {   
        width: 100%;   
        margin: 8px 0;  
        padding: 12px 20px;   
        display: inline-block;   
        border: 2px solid green;   
        box-sizing: border-box;   
    }  
 button:hover {   
        opacity: 0.7;   
    }   
     
 .container {   
        padding: 25px;   
        background-color: white;  
    }   
	
.error {
  color: #a94442;
  background-color: #f2dede;
  border-color: #ebccd1;
  padding:1px 20px 1px 20px;
}
	

Step 3: Add jQuery code to validate


$(function() {
      $("#signup").validate({
      rules: {
        username: {
          required: true
        },
        password: {
          required: true,
		  minlength: 6
        },
        confirm_password: {
          required: true,
		  minlength: 6,
		  equalTo: "#password"
        },
	    email: {
          required: true,
		  email: true
        },
      },
      messages: {
        username: {
          required: "Please enter your username"
        },
        password: {
          required:"Please enter your password",
		  minlength:"Your password must be atleast 6 characters long"
        },
        confirm_password: {
          required: "please enter your password again",
		  minlength: "Your password must be atleast 6 characters long",
		  equalTo:" Both password and confirm password must be same"
        },
	    email: {
          required: "Please enter a valid email address"
        },
		submithandler: function(form){
			form.submit();
		}
}
})
});

Static Variables in JavaScript

Introduction

We Know that JavaScript is also an object oriented programming language and therefore it can also have static variables as well as instance variables.

An instance member belong to a specific instance. If we create 3 instances (objects) there will be 3 copies of instance fields in memory whereas there will ever be only one copy of a static field no matter how many instances we create. In simple words a static variable is a variable which is common for all the objects.

For example:

Public class Circle

{

  // static field

Static float PI= 3.141F;

//instance field

Public int Radius;

}

In the above example , Radius is different for each circle instance you create whereas the PI value remains same and needs to be shared by all the circle objects.

Creating static variables in Javascript:

Let us look at the example below:

function Circle(radius)
{
     this.Radius=radius;
     Circle.PI=3.141;
     this.CalculateArea = function()
     {
         this.result= Circle.PI*this.Radius*this.Radius;
         return this.result;
     } 
  
}
     var circleObject=new Circle(10);
     console.log("Area"+circleObject.CalculateArea());

  let us create a Circle constructor function and pass it radius parameter. Now we create an instance field Radius which is specific to each Circle Object and we create this by using

this.Radius (this keyword).

Next we create a static variable PI which is same for each Circle Object. A static variable can be created by using the name of the constructor function as below:

Circle.PI

Lets create a function this.CalculateArea which calculates the area of the circle.

Note that to invoke a static field we have to use the name of the constructor function.

Creating a virtual environment in Python

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system

Virtual Environment (“virtualenv”) is a tool to create isolated Python environments. It keeps the dependencies
required by different projects in separate places, by creating virtual Python env for them.
“virtualenv” creates a folder which contains all the necessary libs and bins to use the packages that a Python project
would need.

Virtual environment can be installed by using the following command:

pip install virtualenv

Let us look at the usage of the environment we have created.

$ cd myproj // create your directory

$ virtualenv myproj //creating virtual environment

$ source myproj/bin/activate // activate the virtual environment

$ deactivate // to get out of the virtual environment simply type deactivate

Create virtual environment with virtualenvwrapper in windows:

Suppose you need to work on three different projects project A, project B and project C. project A and project B need python 3 and some required libraries. But for project C you need python 2.7 and dependent libraries. So best practice for this is to separate those project environments. For creating separate python virtual environment need to follow below steps:

Step 1: Install pip with this command:

python -m pip install -U pip

Step 2: Install virtualenvwrapper-win

pip install virtualenvwrapper-win

Step 3: Create a new virtualenv environment by using command:

mkvirtualenv <name>

Step 4: Activate the environment by using command:

workon < environment name>

Main commands for virtualenvwrapper:

mkvirtualenv <name> Create a new virtualenv environment named <name>. The environment will be created in WORKON_HOME.

lsvirtualenv List all of the environments stored in WORKON_HOME.

rmvirtualenv <name> Remove the environment <name>.

add2virtualenv <full or relative path> If a virtualenv environment is active, appends <path> to virtualenv_path_extensions.pth inside the environment’s site-packages, which effectively adds <path> to the environment’s PYTHONPATH. If a virtualenv environment is not active, appends <path> to virtualenv_path_extensions.pth inside the default Python’s site-packages. If <path> doesn’t exist, it will be created

Simple calculator program in Python3

Let’s create a simple and basic calculator using python which performs basic operations such as addition , subtraction, multiplication and division.

Program

print('Basic Calculator')

print("Please select operation "+'\n'+
      " 1. Add "+'\n'+
        '2. Subtract'+'\n'+
        '3. Multiply'+'\n'+
        '4. Divide'+'\n' 
        '5.Modulus'+'\n'
        '6.power'+'\n')


# Take input from the user
select = int(input("Select operations form 1, 2, 3, 4,5,6 :"))

number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))

if select == 1:
    print(number_1, "+", number_2, "=",
                    number_1+number_2)

elif select == 2:
    print(number_1, "-", number_2, "=",
                    number_1-number_2)

elif select == 3:
    print(number_1, "*", number_2, "=",
                    number_1*number_2)

elif select == 4:
    print(number_1, "/", number_2, "=",
                    number_1/number_2)
                    
elif select == 5:
	print(number_1,'%',number_2,'=',number_1%number_2)

elif select==6:
	 print(number_1,'**',number_2,'=',number_1**number_2)
	 
else:
    print("Invalid input")

Output

1:

Basic Calculator
Please select operation

  1. Add
  2. Subtract
  3. Multiply
  4. Divide

Select operations form 1, 2, 3, 4 :1
Enter first number: 56
Enter second number: 34
56 + 34 = 90

2:

Basic Calculator
Please select operation

  1. Add
  2. Subtract
  3. Multiply
  4. Divide

Select operations form 1, 2, 3, 4 :3
Enter first number: 4
Enter second number: 5
4 * 5 = 20

3:

Basic Calculator
Please select operation

  1. Add
  2. Subtract
  3. Multiply
  4. Divide
    5.Modulus
    6.power

Select operations form 1, 2, 3, 4,5,6 :6
Enter first number: 4
Enter second number: 2
4 ** 2 = 16

Great applications of Python which motivates you to Master the language

Python is one of the most loved programming languages by developers, data scientists, software engineers because of its versatility, flexibility, and object-oriented features. Python is easy to learn and has a clean syntax which makes it more interesting as compared to other programming languages.

Web and Internet Development

Python can be used to build server-side web applications.This language is widely used for fast prototyping and building highly scalable web applications.

Frameworks: Django, Pyramid, Flask and Bottle

Few of the famous web applications developed using Python are Instagram, Google, Spotify, Netflix, Uber, Dropbox, Pinterest,etc

Scientific and Numeric

Python has built-in support for scientific computing.

Libraries and Packages: SciPy, Pandas,Ipython,

Desktop GUI’s

Python can be used to program desktop applications and in fact in some cases more efficiently than other traditional languages. Python  You can easily and quickly write scripts and simple tests with it.

ToolKits: Tk, WxWidjets ,Kivy, Qt via pyqt or pyside

Software Development

Python is helpful for software development process. It works as a support language and can be used for build control and management, testing etc.

Tools: Scons,Budbot and Apache Gump, Roundup or Trac

Few examples of Softwares developed using Python are An XML Transformation Engine,A custom Image Viewing Game for an Autistic Child,etc

Business Applications

Python is also used to build ERP and e-commerce systems:

Platforms: Odoo, Tryton

Examples: Quora, Reddit, etc

Apart from these there are many more applications of Python. So if you are confused about which programming language to learn then go ahead and start learning Python.

Python was one of the most in-demand technologies of 2018, 2019 and hopefully of 2020. In 2019, it was ranked as the world’s 4th most popular programming language among professional software developers as well as the first most wanted programming language.`

Please like and follow my blog to get the latest news on Python

List of Python IDE’s and code editors

IDE is a development environment which provides many features like coding, compiling, debugging, executing, autocomplete, libraries, in one place for the developer’s thus making tasks simpler whereas Code editor is a platform for editing and modifying the code only.

1: Pycharm

PyCharm is an integrated development environment used in computer programming, specifically for the Python language.

PyCharm is designed by programmers, for programmers, to provide all the tools you need for productive Python development.

The PyCharm IDE is one of the most popular editors used by professional Python developers and programmers. The vast number of PyCharm features doesn’t make this IDE difficult to use–just the opposite. Many of the features help make Pycharm a great Python IDE for beginners.

2:Spyder

Spyder is a powerful scientific environment written in Python, for Python, and designed by and for scientists, engineers and data analysts. It offers a unique combination of the advanced editing, analysis, debugging, and profiling functionality of a comprehensive development tool with the data exploration, interactive execution, deep inspection, and beautiful visualization capabilities of a scientific package.

Because it was built for the data science community, it is integrated with the essential data-centric libraries like NumPy, SciPy, Matplotlib, Pandas, and IPython. The built-in capabilities can be extended further by plugins and APIs.

One of the great features that you would like about Spyder is its Help toolbar, which lets you search a plethora of information related to libraries/modules.

3: Eclipse+ PyDev

PyDev is a Python IDE for Eclipse, which may be used in PythonJython and IronPython development.

It comes with many features such as Django integration , code completion, code completion with auto import, type hinting, code analysis, go to definition, refactoring,etc

If you’ve already got Eclipse installed,adding PyDev will be quicker and easier. PyDev is very accessible for the experienced Eclipse developer. And if you are just starting with Python then it will be difficult to handle.

4: Microsoft Visual studio

Built by Microsoft, Visual Studio is a full-featured IDE. Built for Windows and Mac OS only, VS comes in both free (Community) and paid (Professional and Enterprise) versions.

Visual Studio thinks about your code and tell you which types go where. Tooltips, completions and code snippets make you more productive.

Third-party libraries are the fastest way to solve your problems. Use our pip, PyPI and virtual environment support to manage your projects and dependencies.

None of us write perfect code all the time, but when it goes wrong Visual Studio can help. Visually step through your code, view or modify state, and interact with your program regardless of the operating system.

Manage your Git/TFS projects, pending changes, and branches with ease using Team Explorer, or check out one of the many source control extensions available for Visual Studio. Make sure your code is working correctly without leaving Visual Studio. View, edit, run, and debug unit test-style tests from the Test Window.

5: Sublime Text

Sublime Text is a commonly-used text editor used to write Python code. Sublime Text’s slick user interface along with its numerous extensions for syntax highlighting, source file finding and analysing code metrics make the editor more accessible to new programmers than some other applications like Vim and Emacs.

Sublime Text uses a custom UI toolkit, optimized for speed and beauty, while taking advantage of native functionality on each platform.

Sublime Text is built from custom components, providing for unmatched responsiveness. From a powerful, custom cross-platform UI toolkit, to an unmatched syntax highlighting engine, Sublime Text sets the bar for performance.

6: IDLE

IDLE (Integrated Development and Learning Environment) is an integrated development environment (IDE) for Python. The Python installer for Windows contains the IDLE module by default. IDLE can be used to execute a single statement just like Python Shell and also to create, modify and execute Python scripts.

  • IDLE has the following features:coded in 100% pure Python, using the tkinter GUI toolkit
  • cross-platform: works mostly the same on Windows, Unix, and macOS
  • Python shell window (interactive interpreter) with colorizing of code input, output, and error messages
  • multi-window text editor with multiple undo, Python colorizing, smart indent, call tips, auto completion, and other features
  • search within any window, replace within editor windows, and search through multiple files (grep)
  • debugger with persistent breakpoints, stepping, and viewing of global and local namespaces
  • configuration, browsers, and other dialogs