0

I have an external JS file to validate a form. It all seems to be working. However I added in a regex to validate the password field. As soon as I did this and reloaded the page, none of my previous validation works.. Even with an empty form it submits when it shouldn't. Ive tested it and when I include the variable

var chk_name = ^(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$;

It won't play nice and it submits. But as soon as I take this var out, it starts working and checking there are characters in the fields again. I've tried putting this var in the function, and also outside the function but in both situations its stopping the page from validating properly. Ill give some examples:

This works with no regex:

function validation() {
    var x=document.forms[0]["firstname"].value;
        if (x==null || x=="") {
            alert("First name must be filled out");
            return false;
        }

This doesn't work (ie the form is submitted even with no fields filled out)

var chk_name = ^(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$;

function validation() {
    var x=document.forms[0]["firstname"].value;
        if (x==null || x=="") {
            alert("First name must be filled out");
            return false;
        }
var x=document.forms[0]["password"].value;
        if(x==null || x==""){
            alert("Choose a password at least 6 characters long including 1 upper case letter, 1 lower case letter and 1 number");
            return false;
        } else if(!chk_name.test(x)){
            alert("Choose a password at least 6 characters long including 1 upper case letter, 1 lower case letter and 1 number");
            return false;
        }

This also doesn't work:

function validation() {
    var x=document.forms[0]["firstname"].value;
        if (x==null || x=="") {
            alert("First name must be filled out");
            return false;
        }
var x=document.forms[0]["password"].value;
var chk_name = ^(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$;
        if(x==null || x==""){
            alert("Choose a password at least 6 characters long including 1 upper case letter, 1 lower case letter and 1 number");
            return false;
        } else if(!chk_name.test(x)){
            alert("Choose a password at least 6 characters long including 1 upper case letter, 1 lower case letter and 1 number");
            return false;
        }

1 Answer 1

2

var chk_name = ^(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$;

should be:

var chk_name = /^(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/;

The syntax for literal regex is:

/pattern/flags

You can also use RegExp constructor:

new RegExp("pattern","flags");
Sign up to request clarification or add additional context in comments.

1 Comment

One thing to note, you can specify RegExp in literal notation /[a-z]/ or via constructor new RegExp('[a-z]','modifiers'). The decision on which to use is based upon what you know about the pattern. If you know it is constant use literal if the pattern is going to be dynamic use the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.