Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Regular Expression on Form validation - javascript

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
garethtnash
Asked:
garethtnash
  • 3
  • 3
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
Where is the need for regex here?
0
 
sammySeltzerCommented:
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
 
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

http://www.hotdreamweaver.com/dreamweaver-form-validator
0
 
Michel PlungjanIT ExpertCommented:
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
 
sammySeltzerCommented:
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
 
garethtnashAuthor Commented:
Thank you
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now