Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression on Form validation - javascript

Posted on 2012-04-03
8
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

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

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 29

Accepted Solution

by:
sammySeltzer earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

688 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