Passport Number validation using JavaScript

 Introduction: Hello Guys, “Welcome back” Today, I am here with another one new great article. In this article I explained how to implement Indian Passport Number validation using Regular Expression (Regex) using JavaScript.

Join our Channel  on Whtsapp and Telagram for All Updates                                          

Code for HTML Page

On the  HTML page I will be placed three control one will be  TextBox with the id named txt_passport_number,  second control will be SPAN with the id named lbl_error and a Button with the id named btn_submit. On the OnClick event of the Button i assigned a JavaScript function named validate_passport_number().

Passport Number:

<input type="text" id="txt_passport_number" class="passport" />

<span id="lbl_error" class="error"></span>

<br/>

<input type="button" id="btn_submit"  value="Submit" onclick="validate_passport_number()" />

Validating Passport Number using Regular Expression in JavaScript

When user will be click on the Submit Button then the validate_passport_number() JavaScript function will be executed. Inside the ValidatePassportNumber JavaScript function, the Passport Number TextBox will be referenced by using the id of the textbox and its value will be tested by using a Regular Expression.

The below mentioned conditions must satisfy for the Passport Number to be termed as valid.

1. The Passport Number should be eight characters long.

2. The first character  of Passport Number should be an upper case alphabet with A-Z excluding Q, X, and Z.

3. The second character of Passport Number should be any digit. 1-9.

4. The third character of Passport Number should be any digit. 0-9.

5. The next character of Passport Number should be zero or one white space character.

6. The next four characters of Passport Number should be any digit. 0-9.

7. The last character Passport Number should be any digit. 1-9.

Examples: B3185320,  B38 92068

<script type="text/javascript">

    function validate_passport_number() {

        var passportNumber = document.getElementById("txt_passport_number").value;

        var lbl_msg = document.getElementById("lbl_error");

       lbl_msg.innerHTML = "";

        var expr = /^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/;

        if (!expr.test(passportNumber)) {

           lbl_msg.innerHTML = "Please enter valid Passport Number.";

        }

    }

</script>


CSS Class

The following CSS classes are used.

1. error – It will apply RED color to the error message.

2. passport – It will force the letters to be UPPER case.

<style type="text/css">

    body { font-family: Arial; font-size: 10pt; }

    .error { color: Red; }

    .passport { text-transform: uppercase; }

</style>

Conclusion: In above code, I explained how to implement Indian Passport Number validation using Regular Expression (Regex) using JavaScript. We will come back again shortly with the new article.

Regards

Programming Hub

Automatically download PDF File using JavaScript

Introduction: Hello Guys, “Welcome back” Today, I am here with another one new great article. In this article I explained how to automatically download PDF File using JavaScript. The PDF file will be downloaded as BLOB using XmlHttpRequest AJAX call and then will be sent for download in the Browser using JavaScript. You can also set link or URL of the PDF which can belong to either same server or any other server location. The PDF file will be stored in a folder named download_pdf inside the project directory.

Join our Channel  on Whtsapp and Telagram for All Updates                                          

Automatically download PDF File using JavaScript

Implementation: On the event handler window.onload, the JavaScript function named  download_pdf_file will be  called. Inside the download_pdf_file JavaScript function, the URL or link of the PDF File will be passed through parameter to the GET call of the JavaScript XmlHttpRequest call. After that on the onload event handler, the received Byte Array (Binary Data) is converted to BLOB object and the File is downloaded in Browser. You can copy and paste the whole code on your HTML page to enjoy the program.

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

</head>

<body>

    <script type="text/javascript">

        window.onload = function () {

            setTimeout(function () {

                DownloadFile("Sample.pdf");

            }, 1000);

        };

        function download_pdf_file (fileName) {

            //Set the File URL.

            var url = "download_pdf/" + fileName;

 

            //Create XMLHTTP Request.

            var req = new XMLHttpRequest();

            req.open("GET", url, true);

            req.responseType = "blob";

            req.onload = function () {

                //Convert the Byte Data to BLOB object.

                var blob = new Blob([req.response], { type: "application/octetstream" });

 

                //Check the Browser type and download the File.

                var isIE = false || !!document.documentMode;

                if (isIE) {

                    window.navigator.msSaveBlob(blob, fileName);

                } else {

                    var url = window.URL || window.webkitURL;

                    link = url.createObjectURL(blob);

                    var a = document.createElement("a");

                    a.setAttribute("download", fileName);

                    a.setAttribute("href", link);

                    document.body.appendChild(a);

                    a.click();

                    document.body.removeChild(a);

                }

            };

            req.send();

        };

    </script>

</body>

</html>

Conclusion: In above code, I have been explained how to automatically download PDF File using JavaScript. Bye and take care of you Developers. We will come back again shortly with the new article.

Regards

Programming Hub

 

Insert data into Temp Table using Cursor in SQL Server

Introduction: Hello Guys, “Welcome back” Today, I am here with another one new great article. In this article I explained how to insert data into Temp Table using Cursor in SQL Server.

Join our Channel  on Whtsapp and Telagram for All Updates                                          

Database

I created the following table named userdetails with the all details as follows.

 Column name           data Type

 userid                                   int

 username                 varchar(100)

 useremail                 varchar(50)

useraddress             varchar(250)

I have already inserted few records in the table.

userid       username                 useremail                        useraddress                                      

1                   Sanjeev                  sanjeev@gmail.com    #250 Sector 127, Chandigarh         

2                   Sanjay                    sanjay@gmail.com       #123 Sector 255, Chandigarh         

3                   Ruhika                    ruhika@gmail.com       #256 Sector 855, Chandigarh         

4                   Rajan                      rajan@gmail.com         #55 Sector 00, Chandigarh              

Here I am also giving SQL Server Database Script, you can execute this SQL Database Script by using copy and paste to create table with column and data.

         SET ANSI_NULLS ON

GO

 

SET QUOTED_IDENTIFIER ON

GO

 

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[userdetails](

            [userid] [int] IDENTITY(1,1) NOT NULL,

            [username] [varchar](100) NOT NULL,

            [useremail] [varchar](50) NOT NULL,

            [useraddress] [varchar](250) NOT NULL

           CONSTRAINT [PK_userdetails] PRIMARY KEY CLUSTERED

(

            [userid] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

 

GO

 

SET ANSI_PADDING OFF

GO

I have already inserted few records in the table.

Inserting data into Temp Table using Cursor

Firstly we will declare Temp Table using the columns and we will also declare variables for holding the data. After that we will declare the Cursor with a name and we will set the type of  Cursor as READ_ONLY using with the FOR keyword the SELECT query will also be  written. When Cursor will be set, then it will be opened using the OPEN command and the first record is fetched into the variables. When  a record will be  fetched, them the @@FETCH_STATUS has value 0 and all the records returned by the SELECT query using   Cursor are fetched, its value changes to -1. In the Cursor we will use WHILE loop which will be executes until the @@FETCH_STATUS value becomes 1.

In the WHILE loop the current record will be inserted into the Temp Table and then again the next record will be fetched and so on. Finally, after the fetched of last row of then table, then the Cursor will be closed and deallocated using CLOSE and DEALLOCATE commands  and the SELECT query will be  written to fetch the records from the Temp Table and after selecting the records the Temp Table is deleted. It is compulsory to deallocate a Cursor, otherwise it will not be close from the database and when you declare another Cursor with same name again, SQL Server will throw an error: A cursor with the name 'Cursor1' already exists.

--CREATING TEMP TABLE.

CREATE TABLE #userdetails

(

     [userid] INT

    ,[username] VARCHAR(100)

    ,[useremail] VARCHAR(100)

   ,[useraddressl] VARCHAR(250)

 

)

--DECLARE THE VARIABLES FOR HOLDING DATA.

DECLARE @userid INT

       ,@uername VARCHAR(100)

       ,@useremail VARCHAR(100)

     ,@useraddressVARCHAR(250)

--DECLARE THE CURSOR FOR A QUERY.

DECLARE Insertuser CURSOR READ_ONLY

FOR

SELECT [userid], [username], [useremail] , [useraddress]

FROM userdetails

--OPEN CURSOR.

OPEN Insertuser

--FETCH THE RECORD INTO THE VARIABLES.

FETCH NEXT FROM Insertuser INTO

@userid, @username, @useremail, @useraddress

 --LOOP UNTIL RECORDS ARE AVAILABLE.

WHILE @@FETCH_STATUS = 0

BEGIN

    INSERT INTO # Insertuser

           ([useriId]

           ,[userename]

           ,[useremail]

           ,[useraddress]

)

    VALUES

           (@userid

           ,@username

           ,@ useremail

          ,@ useraddress)

  --FETCH THE NEXT RECORD INTO THE VARIABLES.

     FETCH NEXT FROM Insertuser INTO

     @userid, @username, @useremail, @useraddress

END

--CLOSE THE CURSOR.

CLOSE Insertuser

DEALLOCATE Insertuser

-- SELECT DATA FROM TEMP TABLE

SELECT [CustomerId], [Name], [Country]

FROM #Customers

-- DROP TEMP TABLE

DROP TABLE #Customers

In above code, I explained how to insert data into Temp Table using Cursor in SQL Server. Bye and take care of yourself Developers. We will come back shortly with the new article.

Regards

Programming Hub