Solved

Regular Expression on Form validation - javascript

Posted on 2012-04-03
8
289 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, html5 UI 1 49
Log4Net custom Appender not being fired. 5 44
Select2 jquery help 9 45
C3-->D3 Line Chart 4 18
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

758 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

20 Experts available now in Live!

Get 1:1 Help Now