Welcome to my blog, hope you enjoy reading
RSS
Showing posts with label html5. Show all posts
Showing posts with label html5. Show all posts

Sunday, 23 February 2014

Allow only Numeric in HTML inputbox

Allow only Numeric in HTML inputbox Following code is used to allow only numeric (0-9).

<script language="Javascript">
      function isNumber(evt) {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
         return true;
      }
</script>
 
<input onkeypress="return isNumber(event)" type="text">


Another Way

Following code is used to allow only numeric (0-9).
   
<script language="Javascript">
     function validate(evt) {
      var theEvent = evt || window.event;
      var key = theEvent.keyCode || theEvent.which;
      key = String.fromCharCode( key );
      var regex = /[0-9]|\./;
      if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
    }
</script>
 
<input onkeypress="return isNumber(event)" type="text">

Share

Friday, 9 November 2012

Date time picker in html






Description: Date Time Picker allows your visitors to easily input a date/time into a form field, by selecting it from a popup window. Supports output dates in various formats such as: dd/MM/yyyy, dd/MMM/yyyy, MM/dd/yyyy etc. An extremely versatile script.
Example:
1) Date (MM-dd-yyyy) Pick a date
HTML code: <input id="demo1" type="text" size="25"><a href="javascript:NewCal('demo1','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
2) Date & Time(12-Hour) (dd-MMM-yyyy hh:mm:ss) Pick a date
HTML code: <input id="demo2" type="text" size="25"><a href="javascript:NewCal('demo2','ddmmmyyyy',true,12)"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
3) Date & Time(24-Hour) (dd-MM-yyyy HH:mm:ss) Pick a date
HTML code: <input id="demo3" type="text" size="25"><a href="javascript:NewCal('demo3','ddmmmyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
Directions
1. Download the following zip file, which contains the necessary files plus a demo. 2. Upload "DateTimePicker.js" and "cal.gif" contained within the above zip file to your web site.
3. In the <head> section of your webpage that will display the Date Picker, insert the below code:
<script language="javascript" type="text/javascript" src="datetimepicker.js">

//Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
//Script featured on JavaScript Kit (http://www.javascriptkit.com)
//For this script, visit http://www.javascriptkit.com

</script>
4. Create the desired textboxes that will use the Date Time Picker. A sample textbox looks like:
<input id="demo1" type="text" size="25"><a href="javascript:NewCal('demo1','ddmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
As seen above, enter a unique ID into each <input> tag (in this case, "demo1"), and inside function "NewCal()", pass this ID as its first parameter. Below describes NewCal() in detail:
NewCal([textbox id],[date format],[show time in calendar?],[time mode (12,24)?]) a. You must enclose textbox id and date format with single quotes (').
b. Don't insert date separator in between of date format such as dd-MM-yyyy.
c. Parameters for "NewCal" function:
  • Textbox id (Mandatory)
  • Date format (optional) - If you don't specify date format, the default date format will be "dd-MM-yyyy". Other valid date formats for My Date Time Picker are: ddMMyyyy,MMddyyyy,ddMMMyyyy,MMMddyyyy.
  • Show time in calendar? (optional) [true or false] - Specify "true" if you would like to let user to pick a time. Default value is "false".
  • Time Mode (optional) [12 or 24] - Time shown in 12-hour or 24-hour format. Default is 24-hour format.
Customization If you have little bit of javascript and html knowledge. You will be able to customize the calendar look by yourself.
The first step is to open DateTimePicker.js file itself and you'll find "configurable parameters" section at the header.
//Configurable parameters
var cnTop="200";//top coordinate of calendar window.
var cnLeft="500";//left coordinate of calendar window.
var WindowTitle ="DateTime Picker";//Date Time Picker title.
var WeekChar=2;//number of characters shown for week day. if 2 then Mo,Tu,We. if 3 then Mon,Tue,Wed.
var CellWidth=20;//Width of day cell.
var DateSeparator="-";//Date Separator, you can change it to "/" if you want.
var TimeMode=24;//default TimeMode value. 12 or 24

var ShowLongMonth=true;//Show long month name in Calendar header. example: "January".
var ShowMonthYear=true;//Show Month and Year in Calendar header.
var MonthYearColor="#cc0033";//Font Color of Month and Year in Calendar header.
var WeekHeadColor="#0099CC";//Background Color in Week header.
var SundayColor="#6699FF";//Background color of Sunday.
var SaturdayColor="#CCCCFF";//Background color of Saturday.
var WeekDayColor="white";//Background color of weekdays.
var FontColor="blue";//color of font in Calendar day cell.
var TodayColor="#FFFF33";//Background color of today.
var SelDateColor="#FFFF99";//Backgrond color of selected date in textbox.
var YrSelColor="#cc0033";//color of font of Year selector.
var ThemeBg="";//Background image of Calendar window.
//end Configurable parameters

Share

Thursday, 8 March 2012

HTML5 to connect the SQLite example



Problem

You wold like for your HTML5 application to persist user data in a SQL structure.

Solution

You can use the SQLite datastore available to HTML5 browsers.

Detailed explanation

The following example requires an HTML5 browser. It will create a SQLite database and persist data stored in a table structure.
Let's review the code below.
The page contains some form input fields to hold some information about the user. This includes the firstName, lastName, and phone. There are also buttons to perform the functions of reset, update, insert, and drop. There is also a div with the id or results to display the data.
Now, lets review the process.
Within the JavaScript block, there are variables to hold references to the form elements and the results div. Next, the SQL strings are defined which will be used to interact with the database.
The db variable is defined using the openDatabase function. The database is named AddressBook.
Next, the createTable function is called. Within the createTable function the createStatement is executed with no arguments passed in which will create the table Contacts if it doesn't currently exist. The showRecords method is called when the statement executes successfully. If not, the onError method is called if the statement fails.
Within the showRecords function the results.innerHTML is set to empty string. The selectAllStatament is executed with no variables passed in. On success, the result.rows array is set to the dataset class variable. The dataset is then looped over and the results.innerHTML is written with the lastname, firstname and two links for editing and deleting the contacts.
At this point the page is fully loaded, the AddressBook database and the Contacts table have been created. (See figure 1)
When the user fills out the form and clicks the Insert button, the insertRecord function is called. Within this function the insertStatement is called with the firstName.value, lastName.value, and phone.value arguments passed in. On success the loadAndReset function is called which will reset the form and call the showRecords function which will display the new record within the results div. (See figure 2)
When the edit links is clicked, the loadRecord function is called and the form is populated.
When the delete link is clicked, the deleteRecord function is called. Within the deleteFuction the deleteStatement is executed with the id argument passed in. On success, the showRecords function is called which refreshes the results div.
Finally, there is a drop button which when clicked calls the dropTable function. Within the dropTable function the dropStatement is executed with no arguments passed in.  On success, the showRecords function is called which refreshes the results div.

HTML CODE+JAVASCRIPT

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SQL Storage</title>
 <body>
  <br/><br/>
   <div align="center">
    <input type="hidden" id="id"/>
    First name:<input type="text" id="firstName"/><br/>
    Last name:<input type="text" id="lastName"/><br/>
    Phone: <input type="text" id="phone"/><br/>
    <button onClick="resetForm()">Reset Form</button>
    <button onClick="updateRecord()">Update</button>
    <button onClick="insertRecord()">Insert</button>
       <button onClick="dropTable()">Drop Table</button>
    <div id="results"></div>
   </div>
 </body>

   <script>
var results = document.getElementById('results');
var id = document.getElementById('id');
var firstName = document.getElementById('firstName');
var lastName = document.getElementById('lastName');
var phone = document.getElementById('phone');
 
var createStatement = "CREATE TABLE IF NOT EXISTS Contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, phone TEXT)";
var selectAllStatement = "SELECT * FROM Contacts";
var insertStatement = "INSERT INTO Contacts (firstName, lastName, phone) VALUES (?, ?, ?)";
var updateStatement = "UPDATE Contacts SET firstName = ?, lastName = ?, phone = ? WHERE id = ?";
var deleteStatement = "DELETE FROM Contacts WHERE id=?";
var dropStatement = "DROP TABLE Contacts";

var db = openDatabase("AddressBook", "1.0", "Address Book", 200000);
var dataset;
createTable();

     function onError(tx, error) {
       alert(error.message);
     }
   
     function showRecords() {
       results.innerHTML = '';
       db.transaction(function(tx) {
         tx.executeSql(selectAllStatement, [], function(tx, result) {
dataset = result.rows;
           for (var i = 0, item = null; i < dataset.length; i++) {
             item = dataset.item(i);
             results.innerHTML +=
                 '<li>' + item['lastName'] + ' , ' + item['firstName'] + ' <a href="#" onclick="loadRecord('+i+')">edit</a>  ' +
   '<a href="#" onclick="deleteRecord('+item['id']+')">delete</a></li>';
}
         });
       });
     }
   
     function createTable() {
       db.transaction(function(tx) {
         tx.executeSql(createStatement, [], showRecords, onError);
       });
     }
   
     function insertRecord() {
       db.transaction(function(tx) {
         tx.executeSql(insertStatement, [firstName.value, lastName.value, phone.value], loadAndReset, onError);
       });
     }
   
function loadRecord(i) {
var item = dataset.item(i);
       firstName.value = item['firstName'];
lastName.value = item['lastName'];
phone.value = item['phone'];
id.value = item['id'];
     }

     function updateRecord() {
       db.transaction(function(tx) {
         tx.executeSql(updateStatement, [firstName.value, lastName.value, phone.value, id.value], loadAndReset, onError);
       });
     }
   
     function deleteRecord(id) {
       db.transaction(function(tx) {
         tx.executeSql(deleteStatement, [id], showRecords, onError);
       });
resetForm();
     }
     
     function dropTable() {
       db.transaction(function(tx) {
         tx.executeSql(dropStatement, [], showRecords, onError);
       });
resetForm();
     }

function loadAndReset(){
resetForm();
showRecords();
}

function resetForm(){
firstName.value = '';
lastName.value = '';
phone.value = '';
id.value = '';
}
   </script>
</html>​

figure1:
figure2:

Share