...

View Full Version : Form validation regexp



virtualbob
03-26-2005, 02:56 AM
I'm using a simple Javascript form validator (form_validator.js available from http://www.javascript-coder.com/). I quite like it as its straight forward and easy to create custom error strings. However, I want it to match a set string (reserved word that is an example of a password that I don't want people to actually use)

It has a regexp option that is explained as:

regexp=??? Check with a regular expression; the data entered should match the regular expression.
Example: "regexp=^[A-Za-z]{1,20}$" allow up to 20 alphabetic characters.

I just can't get anything to work next to "regexp= such as "regexp=/foo/" Anyone know what would work here to match foo or (foo OR bah)?

The reference in the script is:

case "regexp":
{
if(objValue.value.length > 0)
{
if(!objValue.value.match(cmdvalue))
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Invalid characters found ";
}//if
alert(strError);
return false;
}//if
}
break;
}//case regexp

+++++++++++++

Alternatively can the below be hacked down to do what I want? I'm sure I could create 2 custom validators with a sort of field.match but I don't know what to delete from the below:

+++++++++++++
function validateEmailv2(email)
{
// a very simple email validation checking.
// you can add more complex email checking if it helps
if(email.length <= 0)
{
return true;
}
var splitted = email.match("^(.+)@(.+)$");
if(splitted == null) return false;
if(splitted[1] != null )
{
var regexp_user=/^\"?[\w-_\.]*\"?$/;
if(splitted[1].match(regexp_user) == null) return false;
}
if(splitted[2] != null)
{
var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null)
{
var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if(splitted[2].match(regexp_ip) == null) return false;
}// if
return true;
}
return false;
}
function V2validateData(strValidateStr,objValue,strError)
{
var epos = strValidateStr.search("=");
var command = "";
var cmdvalue = "";
if(epos >= 0)
{
command = strValidateStr.substring(0,epos);
cmdvalue = strValidateStr.substr(epos+1);
}
else
{
command = strValidateStr;
}
switch(command)
{
case "req":
case "required":
{
if(eval(objValue.value.length) == 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : Required Field";
}//if
alert(strError);
return false;
}//if
break;
}//case required

Thanks,

VB

Philip M
03-26-2005, 07:32 AM
test RegExp tests for a match in a string, and returns true or false.

e.g.

if (/invalidpassword/gi.test(str.value)) {
Alert ("Invalid Password");
}

virtualbob
03-26-2005, 11:47 AM
Thanks but I don't understand this - where do I insert the fieldname name and what do I build around it to make a complete piece (I'm not a javascript coder)

if (/invalidpassword/gi.test(str.value)) {
Alert ("Invalid Password");
}

Philip M
03-26-2005, 08:04 PM
invalidpassword is supposed to be the literal password (the reserved word you mention) that you want to reject. It would be simpler if you told us what this word actually is.

e.g. mypassword

str is supposed to be the string which the user enters as his password.

So in English "test the input string str to see if it contains mypassword, ignoring case, and if it does bring up the alert message".

Of course, if the test is true you will need to set

str.value=""; // the input string is set to nothing
str.focus(); // and set the focus back in the input box



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum