|
|
|
|
|
|
| |
Sometimes it becomes necessary to process user input client side. It saves the user a lot of time if he/she is notified of errors in his/her errors before submitting the form. This example contains two functions for validating email addresses and urls.
|
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>JavaScrip URL & Email Validation</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
<!--
function isValidURL(url){
var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
if(RegExp.test(url)){
return true;
}else{
return false;
}
}
function isValidEmail(email){
var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
if(RegExp.test(email)){
return true;
}else{
return false;
}
}
function checkField(){
var frm = document.frmValidate, error = "";
if(!isValidURL(frm.url.value)){
error += "Please enter a valid URL\n";
}
if(!isValidEmail(frm.email.value)){
error += 'Please enter a valid Email\n';
}
if(error != ""){
alert(error);
return false;
}else{
return true;
}
}
//-->
</script>
</head>
<body>
<table height="100%" width="100%">
<tr><td>
<center><form name="frmValidate" onsubmit="return checkField()">
<table cellspacing="2" cellpadding="5" border="1">
<tr><td>Enter Valid URL : </td>
<td><input type="text" name="url"></td></tr>
<tr><td>Enter Valid Email : </td>
<td><input type="text" name="email"></td></tr>
<tr><td colspan="2"><center><input type="submit" value="Check"></center></td></tr>
</table>
</form></center>
</td></tr></table>
</body>
</html> | | |
|
| Form Processing : with alert Highlight field name which is not filled by user Categories : Java Script, Form Processing, Data Validation, Beginner Guides, Web Design | | | Adding a Filter to a Text Element Categories : Java Script, Data Validation, Form Processing | | | Email Address Protection (Avoide SPAM) Categories : Java Script, Email, Encryption | | | PHPRecommend v1.0 - "Recommend this page to a friend" script written in
PHP. Easy to install Categories : PHP, URLs, Complete Programs, Email, Site Planning | | | Validating a URL with JavaScript RegExp Categories : Java Script, Data Validation, Beginner Guides | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | | | send php mail with form data and attachment. Categories : PHP, Email, Mail, Form Processing | | | Form Validation Using PHP to highlight non valid fields Categories : PHP, Form Processing, Data Validation, Beginner Guides | | | Hilight Form Element onFocus Categories : Java Script, Form Processing, CSS | | | Conditional Check - a script that allows a user to submit a form only if the user check a checkbox. Categories : HTML, Java Script, Form Processing, Beginner Guides | | | Form validations using javascript and including all validations in one message Categories : HTML and PHP, Java Script, Form Processing | | | showing Help (assistance) to the user while filling html forms. Categories : HTML, Java Script, Form Processing | | | cPanel Email Accounts Creator Categories : PHP, PHP Classes, Email, Form Processing, Web Services | | | PHP and javascript mouseover, mouseout, and mousedown events Categories : PHP, Java Script, Form Processing, Beginner Guides | | | Upload any fixed type of files, control file type through javascript and encrypt filename using php so file not get overwrite Categories : PHP, Java Script, Functions, PHP References, Form Processing | |
| | | | Tim N wrote :1805
the URL regex does not work for URLs with commas in them (ex: http://www.latimes.com/news/local/la-me-textbook18-2008aug18,0,7294220,full.story)
this one works for me on those url's
var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([,-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([,-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([,-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
(note: logged in using bugmenot, so don't bother trying to contact me)
| |
|
|