Google Answers Logo
View Question
 
Q: Javascript coding ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Javascript coding
Category: Computers > Programming
Asked by: xxbifferxx-ga
List Price: $10.00
Posted: 08 Apr 2004 10:31 PDT
Expires: 08 May 2004 10:31 PDT
Question ID: 327211
Find a function in javascript that will check a password of any length
has at least 2 numeric characters.

Clarification of Question by xxbifferxx-ga on 08 Apr 2004 10:41 PDT
2 passwords fields have to be compared to be Verified and there must
be at least 2 numeric characters
Answer  
Subject: Re: Javascript coding
Answered By: googleexpert-ga on 08 Apr 2004 12:01 PDT
Rated:5 out of 5 stars
 
Hi xxbifferxx,
Below is a function that checks if Both Textfields match AND if they
have 2 Numeric Characters:

<html>
<script language="JavaScript">
function Check()
{
var pass = document.frm.txt.value;
var pass2 = document.frm.txt2.value;
//Check if 2 Passwords Match contain 2 Numeric Characters
if( (pass == pass2)&&(pass.match(/[0-9].*?[0-9]/g)) )
{	alert("Matched!");	}
else
{	alert("No Match");	}
}
</script>
<table>
<form name="frm" onSubmit="Check();">
<tr>
<td>Enter Your Password: </td>
<td><input type="text" name="txt" size="30"></td>
</tr>
<tr>
<td>Enter Your Password Again:</td>
<td><input type="text" name="txt2" size="30"></td>
</tr>

<tr><td>
<input type="submit" name="submit" value="Password Check">
</td></tr>
</form>
</table>

</html>

Please let me know if you have anymore questions.
Thank you.
-googleexpert

Request for Answer Clarification by xxbifferxx-ga on 09 Apr 2004 11:09 PDT
Excellent! Big thanks worked well! I?d just like to know if I wanted
the pass.match(/[0-9].*?[0-9]/g)) != true, then alert("No Matched!");
how would I modify it as the one I mentioned doesn?t work.

Clarification of Answer by googleexpert-ga on 09 Apr 2004 21:33 PDT
Hi,
I made some changes to the Javascript Function.
Please let me know if there are any problems.
Thank you.
-googleexpert

<html>
<script language="JavaScript">
function Check()
{
//Retrieve 2 Textfield Values
var pass = document.frm.txt.value;
var pass2 = document.frm.txt2.value;

//Remove Spaces
pass  = pass.replace(/\s+/g,"");
pass2 = pass2.replace(/\s+/g,"");

//Match for 2 Numeric Characters in Password
var matched = pass.match(/\d.*?\d/g);

//Check if Two Passwords Match and contain 2 Numeric Characters
if( (pass == pass2)&&(matched) )
{       alert("Matched!");      }

if(!matched )
{       alert("No Match");      }

}
</script>
<table>
<form name="frm" onSubmit="Check();">
<tr>
<td>Enter Your Password: </td>
<td><input type="text" name="txt" size="30"></td>
</tr>
<tr>
<td>Enter Your Password Again:</td>
<td><input type="text" name="txt2" size="30"></td>
</tr>

<tr><td>
<input type="submit" name="submit" value="Password Check">
</td></tr>
</form>
</table>

</html>
xxbifferxx-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
Big thanks! This was a great help.

Comments  
Subject: Re: Javascript coding
From: lucos-ga on 08 Apr 2004 11:54 PDT
 
Try...

function checknum(pass)
{
numbers = new Array(1,2,3,4,5,6,7,8,9,0);
flag=0;

for(i=0;pass.length;i++)
{
   char = charAt(i);
   for(i=0;i<10;i++)
   {
      if(char == numbers[(i)])
      {
      flag++;
      }
   }
}

if(flag > 2)
{
  alert("This password has at least 2 numbers"); // OPTIONAL
}else{
  alert("Sorry, invalid entry"); // PUT ERROR EVENT HERE
}

}

if(pass1 == pass2)
{
checknum(pass1);
checknum(pass2);
}else{
alert("Passwords are not the same");
}

Not tested, so probably better waiting for one of the researchers
Subject: Re: Javascript coding
From: xxbifferxx-ga on 23 Apr 2004 10:50 PDT
 
tried to make it work but it failed, probably me but thanks for your comment

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy