Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Form validation regexp

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    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");
    }

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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");
    }

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •