Archive for the ‘Javascript’ Category

Javascript compare two numbers

How to compare two Value in JavaScipts:

1.Get the Value From Html Page using getElementById

2.Compare by Using parseInt For Interger Value and parseFloat For Float Value

3.compare two value as

var first=document.getElementById('first').value;
var second=document.getElementById('second').value; 
 
if(parseInt(first)<parseInt(second))
alert('Second Value is Greater than first');
 
else if(parseInt(first)>parseInt(second))
alert('First Value is Greater than second');
 
else if(parseInt(first)==parseInt(second))
alert('First Value and second value is equal');

4.Include the code in Html Page For TextBox

First Value:<input type="text" value="0" onkeyup="compare();" name="first" id="first">
Second Value:<input type="text" value="0" onkeyup="compare();" name="second" id="second">

Demo Link

Popularity: 51% [?]

JavaScript to Validate Email Format

To validate the email format,  check the textbox value across the regular expression value. The javascript code and corresponding html code are:

Script :

var efmt=/^\w+((-\w+)(\.\w+))*\@[A-Za-z0-9]+((\.-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;  
 
function checkEmailFormat(field,msg)
{
  var ctrl=document.getElementById(field);
  if((!efmt.test(ctrl.value)) &amp;&amp; ctrl.value!=)
  {
    alert(msg);
    ctrl.focus();
  }
}

Add the following code into the Body tag.
Html Code :

<input type=”Text” onblur=”checkEmailFormat(’Txtemail’,'Please enter a valid email address, like sample@yahoo.com’);” id=”Txtemail” >; 

Demo Link

Popularity: 5% [?]

set focus on textbox value at the end

To set focus on textbox Value at the End using javascript follow the steps:

1.Assuming ‘Search’ is the id given for the Textbox.
2.setFocus() function called on BODY load.
3.The function will position the cursor at the end of the Textbox value
(if any value Present it will point at the end of the value)

function setFocus()
{
var field = document.form.Search;
field.focus();
field.value = field.value;
field.focus();
}

 

Demo Link

Popularity: 1% [?]

Refresh the Window

To refresh the window using JavaScript

window.location.reload();

 

Popularity: 1% [?]

Set textbox background color when clicked

To set the background color of the textbox, use the ‘backgroundColor’ property.
eg.

<input type="text" style='background:#fce09a' value='www.fordevs.com'>

If you want to set the background color on text focus then use the ‘onfocus’ event.

The following example illustrates you how to set the color in ‘onfocus’ event. When the texbox loses the focus then normal background color is retained.

<html>
<body>
<br><input class="searchInput" id="searchtextbox" onblur="this.style.backgroundColor='#ffffff';"
onfocus="this.style.backgroundColor='#fce09a';value=''" accesskey="s" value="[Enter your name]" name="searchtextbox" autocomplete="off"> <br>
<br>
</html>

Demo link

Popularity: 3% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars