Solved

Regular Expression on Form validation - javascript

Posted on 2012-04-03
8
293 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37809825
Can you post a link to the validator documentation?
0
 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

820 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