Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Chapter 6: Beginning JavaScript

Controlling Data Entry Using Form Fields

Form Image Button Fields

Creating 'Encoded' Name & Value Pairs

Disabling form elements

Passing data from one form to another

Dynamic Dropdown Menus

Form Tricks

Dropdown Menus #3

Check Boxes and Radio Buttons

Addressing Form Field Validation with Regular Expressions and JavaScript 1.2- source

You are here: irt.org | Articles | JavaScript | Form | Addressing Form Field Validation with Regular Expressions and JavaScript 1.2 [ previous next ]

Published on: Sunday 16th November 1997 By: Jason Nugent

Return

<html>
<head>

<title>Regular Expressions for Form Field Validation</title>

<script language="JavaScript"><!--
function isEmail(string) {

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
}                      
function isProper(string) {

   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
}                      
function isReady(form) {
    if (isEmail(form.address.value) == false) {
        alert("Please enter a valid email address.");
        form.address.focus();
        return false;
    }
    if (isProper(form.username.value) == false) {
        alert("Please enter a valid username.");
        form.username.focus();
        return false;
    }
    return true;
}
//--></script>

<script language="JavaScript1.2">
function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function isProper(string) {
    if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
}
//--></script>

</head>

<body>

<form name="form_name" onSubmit="return isReady(this)">
<table cellpadding=0 cellspacing=5 border=0><tr>
    <td align="left">Your Name:</td><td align="left"><input type="text" name="username"></td>
</tr><tr>
    <td align="left">Your Email Address:</td><td align="left"><input type="text" name="address"></td>
</tr><tr>
    <td><input type="submit" value="Submit"></td>
</tr></table>
</form>

</body>
</html>

Return

Related items

Chapter 6: Beginning JavaScript

Controlling Data Entry Using Form Fields

Form Image Button Fields

Creating 'Encoded' Name & Value Pairs

Disabling form elements

Passing data from one form to another

Dynamic Dropdown Menus

Form Tricks

Dropdown Menus #3

Check Boxes and Radio Buttons

©2018 Martin Webb