Chat > Tech Chat

javascript regex

(1/1)

chenks:
anyone any good at javascript regex?

i'm trying to add validation on an html form field, all is fine except for one particular field.
this field only requires a value when the preceeding form field is answered as "YES", and when it is required it can only be numbers (0-9).

at the moment i have it split across two checks - which obviously isn't ideal.
the first check works but the second check is kicking in even when the answers is "NO" on the preceeding field.


--- Code: ---if ((x7 == "YES") && (x7b == null || x7b == "")) {
    alert("Heat Control Estimated LTS must be entered");
    return false;
}
   
if ( (x7 == "YES") && (/^[0-9]+$/.test(x7b)) ){
} else {
    alert("Invalid Heat Control Estimated LTS; must only be numbers");
    return false;
}

--- End code ---

so 1) can i combine the check into one? and 2) why is the second check not working as expected?

Navigation

[0] Message Index

Go to full version