Solved

Regular Expression on Form validation - javascript

Posted on 2012-04-03
8
291 Views
Last Modified: 2012-08-14
Hi Experts,

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

<script>
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(); 
}
</script>

Open in new window


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

EnewsAccountType
EnewsStartDate
EnewsUN
EnewsPWD

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??

Thanks

G
0
Comment
Question by:garethtnash
  • 3
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37804526
Where is the need for regex here?
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 37807545
If you are having issues incorporating these:

EnewsAccountType
EnewsStartDate
EnewsUN
EnewsPWD

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.
0
 

Author Comment

by:garethtnash
ID: 37809804
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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37809825
Can you post a link to the validator documentation?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:garethtnash
ID: 37809835
Sure this is where I got it from --

http://www.hotdreamweaver.com/dreamweaver-form-validator
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37809871
Not a clue.

No documentation mentions conditional validation.

Try asking the developers.

http://www.hotdreamweaver.com/contact-us

I would personally use the jQuery validator. It is much more documented and many more people know about it and it is free
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 37811523
Why not use something this simple:

<html>
<head>

<style type="text/css">

.text { }

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

</style>

<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";
error[i].select();
}

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

</script>

</head>
<body>

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

</form>

</body>
</html>

Open in new window

See?

You can easily add more form elements.
0
 

Author Closing Comment

by:garethtnash
ID: 37880207
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now