Regular Expression on Form validation - javascript

Hi Experts,

I've got a neat javascript that validates elements on my page;

function validateform1(formNode){
  var formValidatorInstance = new FormValidator(formNode);
  formValidatorInstance.addRule('MYTGIUN','MYTGI "Username" - value is required','required');
  formValidatorInstance.addRule('MYTGIPWD','MYTGI "Password" - value is required','required');
  formValidatorInstance.addRule('FORUMUN','Forum "Username" - value is required','required');
  formValidatorInstance.addRule('FORUMPWD','Forum "Password" - value is required','required');
  formValidatorInstance.addRule('PGUN','Price Guide "Username" - value is required','required');
  formValidatorInstance.addRule('PGPWD','Price Guide "Password" - value is required','required');
  formValidatorInstance.addRule('OLPUN','Online Print "Username" - value is required','required');
  formValidatorInstance.addRule('OLPPWD','Online Print "Password"- value is required','required');
  formValidatorInstance.addRule('TGIDUN','TGI Direct "Username"- value is required','required');
  formValidatorInstance.addRule('TGIDPWD','TGI Direct "Password" - value is required','required');
  return formValidatorInstance.validate(); 

Open in new window

I really need for this to do a little more, in that I have an additional 4 elements =


I would like for the above script to include a regex that states something like

if(document.getElementById("liEnewsStartDate") !=""){
  formValidatorInstance.addRule('EnewsStartDate','ENews "Start Date"- value is required','required');
  formValidatorInstance.addRule('EnewsUN','ENews "Username"- value is required','required');
  formValidatorInstance.addRule('EnewsPWD','ENews "Password" - value is required','required');

Is there a simple way of doing this??


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michel PlungjanIT ExpertCommented:
Where is the need for regex here?
If you are having issues incorporating these:


into your current script, let us know.

Otherwise, do all in regex or do all the same way you are doing yours.

Really, as mplungjan stated, they don't have to be done in regex at all.
garethtnashAuthor Commented:
Hi Sorry, you are of corse both right, no regex here.

What I was hoping to do was incorporate the additional rules into the script, only if EnewsAccountType is not ""

Any thoughts gratefully appreciated -

Thank you
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Michel PlungjanIT ExpertCommented:
Can you post a link to the validator documentation?
garethtnashAuthor Commented:
Sure this is where I got it from --
Michel PlungjanIT ExpertCommented:
Not a clue.

No documentation mentions conditional validation.

Try asking the developers.

I would personally use the jQuery validator. It is much more documented and many more people know about it and it is free
Why not use something this simple:


<style type="text/css">

.text { }

.text1 { background-color: #F0E68C;border:2px inset orange; }


<script type="text/javascript">

function validate(form1)
var error = new Array();
var valid = new Array();

if(form1.firstname.value.length == 0) error[error.length] = form1.firstname;
else valid[valid.length] = form.firstname;

if(form1.lastname.value.length == 0) error[error.length] = form1.lastname;
else valid[valid.length] = form1.lastname;

if(form1.username.value.length == 0) error[error.length] = form1.username;
else valid[valid.length] = form1.username;

if(form1.password.value.length == 0) error[error.length] = form1.password;
else valid[valid.length] = form1.password;

if(valid.length > 0)
for(var j = 0; j < valid.length; j++) valid[j].className = "text";

if(error.length > 0)
for(var i = 0; i < error.length; i++)
error[i].className = "text1";

 alert("Please fill out the red highlighted fields!");
return false;



<form id="form1" action="" name="buchen" method="post" onsubmit="return validate(this);">
   <td>First Name*</td>
   <td><input id="firstname" class="text" type="text" name="firstname" size="15"/></td>
<td>Last Name*</td>
<td><input id="lastname" class="text" type="text" name="lastname" size="15"/></td>
<td><input id="username" class="text" type="text" name="username" size="15"/></td>
<td><input id="password" class="text" type="text" name="password" size="15"/></td>
<p class="submit">
<button name="submit" type="submit" value="Submit">Submit</button>



Open in new window


You can easily add more form elements.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garethtnashAuthor Commented:
Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.