Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation: Must have one number and one letter

Status
Not open for further replies.

dzirkelb

Programmer
Jan 2, 2007
14
US
I have a page with one validation in place (must be only numbers), but I would like to add another validation. The second textbox, txtLocator, must have at least 1 number and at least 1 letter to be validated. So, it could be
1M, 1M13, 5RRR34, etc etc...5 would fail, 555 would fail, MMM would fail, M would fail. Any ideas?

Here is my code for the page currently.

Code:
<html>

<head>
<title>Full Warehouse Scan</title>
</head>

<body OnLoad="SetFocus()">

<form id="frmGeneral" name="frmGeneral" action="FullWarehouseScan.asp" method="post">
	<table border="0">
		<tr>
			<td><font face="verdana" size="1">Part ID</font></td>
			<td><input type="text" id="txtPartID" name="txtPartID" size="20" onChange="ValidateNumber()" style="font-family: Verdana; font-size: 8pt"></td>		
		</tr>
			<td><font face="verdana" size="1">Locator</font></td>
			<td><input type="text" id="txtLocator" name="txtLocator" size="20" style="font-family: Verdana; font-size: 8pt"></td>
		</tr>
	</table>
</form>

</body>
</html>

<Script Language="JavaScript">
<!--
function SetFocus()
         {
			document.frmGeneral.txtPartID.focus();
         }
function ValidateNumber()
	{
		if(isNaN(frmGeneral.txtPartID.value))
		{
			alert("You must enter a numeric value for a Part ID");
			document.frmGeneral.txtPartID.focus();
			return(false);
                          }
	}
//-->
</Script>
 
Code:
function isValidStr(str) {
  hasNumber = false;
  hasAplha = false;
  badStr = false;
  goodNumberStr = "1234567890";
  goodAlphaStr = "abcdefghijklmnopqrstuvwxyz";
  for(i = 0; i<str.length && !badStr; i++) {
    if(goodNumberStr.indexOf(str.charAt(i)) >= 0) {
      hasNumber = true;
    } else if(goodAlphaStr.indexOf(str.charAt(i).toLowerCase()) >= 0) {
      hasAlpha = true;
    } else {
      badStr = true;
    }
  }
  if(badStr || !hasAlpha || !hasNumber) {
    return false;
  }
  return true;
}
 
I got this to work with the following function:

Code:
function ValidateLocator()
	{
		if(/\d{1}[A-z]|[A-z]{1}\d/.test(frmGeneral.txtLocator.value))
		{
			frmGeneral.submit();
		}else{
			alert("You entered an invalid Locator");
			document.frmGeneral.txtLocator.focus();
			return(false);
		}
	}

Thnks!
 
All of the "pass" conditions you listed above started with numbers, but you did not specify that in your explanation. So, this example was built with the idea that it did not have to start with a number. If it does, that is easy enough to fix. Additionally, you stated with the first validation that it had to be only numbers, but the second you didn't state that it had to be only letters and numbers - merely that it had to have one of each. Again, I assumed for this example that it could have only letters and numbers (no special characters) - this is another easy fix if it is incorrect.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function validate(str) {
   if (/^([A-Za-z]+\d+[A-Za-z0-9]*)|(\d+[A-Z]+[A-Za-z0-9]*)$/.test(str)) {
      alert(str + " is a valid string");
   }
   else {
      alert(str + " is not a valid string");
   }
}

validate("abc1234abc");
validate("abc");
validate("123");
validate("1a*b1");

</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
I'd suggest using either dzirkelb's or my solution, as the regular expressions will cut down on your code and execution time considerably. Take note that dzirkelb's solution will allow "special" characters.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Ya, sorry, my explanation wasn't the best after re-reading it...but it made sense to me! Thanks for the code :)
 
I'd suggest using either dzirkelb's or my solution

Gah.... I didn't realize you were the OP, lol. That's funny that I gave you the suggestion to use your own code [lol]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top