Posted: Wed, Mar 23 05:27 AM (PDT)
The window.location object is a JavaScript class that is used to store URLs. It comes with properties that represent each part of the URL, and can be updated by changing the href property. The key properties that this article deals with are:
- window.location.href - the full URL address
- window.location.protocol - the protocol what you use http or https
- window.location.host - the hostname (for example localhost or www.atlas365.com or different)
- window.location.pathname - the parts of the URL (for example index.html)
In addition there are various methods that:
- window.location.reload() - the function for reload current webpage
- window.location.replace(url) - the function for change URL address // for example window.location.replace("http://www.atlas365.com");
Tags:
Posted: Tue, Mar 22 04:14 PM (PDT)
This page shows a technique for restricting the field to just numbers. The script also allows you to automatically jump to the next field if the user presses the decimal key.
Put this JavaScript to head tag in your HTML page
<script type="text/javascript">
<!--
function numbersonly(inputfield, e, dec) {
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27))
return true;
else if ((("0123456789").indexOf(keychar) > -1))
return true;
else if (dec && (keychar == ".")) {
inputfield.form.elements[dec].focus();
return false;
}
else
return false;
}
//-->
</script>
Now, we will use our script in HTML form
<form action="/signup" method="post">
<label>U.S. ZIP Code:</label><input type="text" name="zip" size="5" maxlength="5" value="" onKeyPress="return numbersonly(this, event)"/>
<button type="submit">Submit</button>
</form>
Tags:
Posted: Tue, Mar 8 05:50 PM (PST)
An easy way to validate credit card number with one function in JavaScript
function isValidCardNumber(cardNumber) {
var nCheck = 0;
var nDigit = 0;
var bEven = false;
for (n = cardNumber.length - 1; n >= 0; n--) {
var cDigit = cardNumber.charAt(n);
if (isDigit(cDigit)) {
var nDigit = parseInt(cDigit, 10);
if (bEven) {
if ((nDigit *= 2) > 9)
nDigit -= 9;
}
nCheck += nDigit;
bEven = ! bEven;
}
else if (cDigit != ' ' && cDigit != '.' && cDigit != '-') {
return false;
}
}
return (nCheck % 10) == 0;
}
function isDigit(c) {
var strAllowed = "1234567890";
return (strAllowed.indexOf(c) != -1);
}
function isCardTypeCorrect(cardNumber, type) {
var nLen = 0;
for (n = 0; n < cardNumber.length; n++) {
if (isDigit(cardNumber.substring(n, n + 1)))
++nLen;
}
if (type == 'Visa')
return ((cardNumber.substring(0, 1) == '4') && (nLen == 13 || nLen == 16));
else if (type == 'Amex')
return ((cardNumber.substring(0, 2) == '34' || cardNumber.substring(0, 2) == '37') && (nLen == 15));
else if (type == 'Master Card')
return ((cardNumber.substring(0, 2) == '51' || cardNumber.substring(0, 2) == '52'
|| cardNumber.substring(0, 2) == '53' || cardNumber.substring(0, 2) == '54'
|| cardNumber.substring(0, 2) == '55') && (nLen == 16));
else
return false;
}
Tags:
Posted: Tue, Mar 8 05:32 PM (PST)
Here is the code to validate email address in JavaScript using regular expression.
function validate(form_id,email) {
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.forms[form_id].elements[email].value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
In the forms 'onsubmit' code call javascript:return validate('form_id','email_field_id')
<form id="form_id" method="post" action="action.php" onsubmit="javascript:return validate('form_id','email');">
<input type="text" id="email" name="email" />
<input type="submit" value="Submit" />
</form>
You should not rely purely on client side validation on your website / web application, if the user has javascript disabled this will not work. Always validate on the server.
Tags:
Posted: Tue, Mar 8 11:56 AM (PST)
The JavaScript function for String Replace replaces the first occurrence in the string. The function is similar to the php function str_replace and takes two simple parameters. The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found. The javascript function does not Replace All...
str = str.replace("find","replace")
To ReplaceAll you have to do it a little differently. To replace all occurrences in the string, use the g modifier like this:
str = str.replace(/find/g,"replace")
Tags: