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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help me validate this textbox...

Status
Not open for further replies.

r0nke

MIS
Jul 17, 2003
60
US

I need to validate a text box.

the value of this textbox could be:

12 characters long
(only between "0123456789abcdefghABCDEFGH")
but 5th digit must be 4 or 6

or

15 characters long - first 3 digits must be '151' and the rest anything between 0123456789abcdefghABCDEFGH


or

12 characters long
if first digit is 6, then
second character must be d, e, or f
and the rest could be any alphanumberic character.

I need to validate all these against ONLY ONE text box.

Thanks guys



 
<script type = &quot;text/javascript&quot;>

var notAlphaNum = /[^a-zA-Z0-9]/;

function A(){

var lgn = document.a.b.value.length;alert(lgn);
var txt = document.a.b.value;
var nmb = 151;

if(lgn != 12 && lgn != 15){
alert(&quot;Wrong number of chars.&quot;);
return;
}

if(txt.match(notAlphaNum)){
alert(&quot;Non Alpha Numeric Character&quot;);
return;
}

if(lgn == 12){
if(!(txt.substr(4,1) == &quot;4&quot; && txt.substr(4,1) == &quot;6&quot;)){
alert(&quot;Wrong number in position 4.&quot;);
return;
}
}

if(lgn == 15){
if(!(txt.substr(0, 3) == nmb)){
alert(&quot;First 3 chars incorrect&quot;);
return;
}
}
}

</script>
 
I copied these ideas from:


These functions check length of string (5) and validates
1st four chars to be numbers, last char aA,Bb or cC.
Such as: 5656B

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function ckID(form) {
Ctrl = document.meter.ID;
TestAlph = isAlphString (Ctrl.value.charAt(4));
TestNum = isNumString (Ctrl.value.substring(0,4));
if (TestAlph == 0 || TestNum == 0 || Ctrl.value.length != 5) {
validatePrompt (Ctrl, &quot;Please enter a valid Equipment ID Number...&quot;)
return (false);
} else
return (true);
}
function isAlphString(InString) {
var RefString=&quot;AaBbMm&quot;;
for (Count=0; Count < InString.length; Count++) {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1)
return (false);
}
return (true);
}
function isNumString(InString) {
var RefString=&quot;1234567890&quot;;
for (Count=0; Count < InString.length; Count++) {
TempChar= InString.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1)
return (false);
}
return (true);
}

Hope this helps, the web-site above is a great resource!
Lrnmore
 
A correction to my post above

if(!(txt.substr(4,1) == &quot;4&quot; && txt.substr(4,1) == &quot;6&quot;)){

should be-

if(txt.substr(4,1) != &quot;4&quot; && txt.substr(4,1) != &quot;6&quot;)
 
Forgot about the d, e, f bit- here is a more complete one-

<html>
<head>
<title>Form test</title>
<script type = &quot;text/javascript&quot;>

var notAlphaNum = /[^a-zA-Z0-9]/;

function A(){

var lgn = document.a.b.value.length;alert(lgn);
var txt = document.a.b.value;
var nmb = 151;

if(lgn != 12 && lgn != 15){
alert(&quot;Wrong number of chars.&quot;);
return;
}

if(txt.match(notAlphaNum)){
alert(&quot;Non Alpha Numeric Character&quot;);
return;
}

if(lgn == 12){
if(txt.substr(4,1) != &quot;4&quot; && txt.substr(4,1) != &quot;6&quot;){
alert(&quot;Wrong number in position 4.&quot;);
return;
}

if(txt.substr(0, 1) == 6){
if(txt.substr(1,1) != &quot;d&quot; && txt.substr(1,1) != &quot;e&quot; && txt.substr(1,1) != &quot;f&quot;){
alert(&quot;Incorrect character at position 1.&quot;);
return;
}

}

}

if(lgn == 15){
if(!(txt.substr(0, 3) == nmb)){
alert(&quot;First 3 chars incorrect&quot;);
return;
}
}
}

</script>


</head>

<body bgcolor=&quot;#ffffff&quot;>


<form name = &quot;a&quot;><input type = &quot;text&quot; name = &quot;b&quot; onblur = &quot;A()&quot;></form>

</body>

</html>
 
wow... rather complex... I'll get you started, though... Hopefully somebody with more regular expression knowledge than I have will come along and complete this for us... ;-)

function validateString() {
var strValue = document.form1.textboxname.value;
var isValid = false;

if (strValue.length == 12) {
if (strValue.substr(0,1) == '6') {
if (strValue.substr(1,1) == 'd' || strValue.substr(1,1) == 'e' || strValue.substr(1,1) == 'f') {
isValid = true;
}
} else {
if (strValue.substr(4,1) == '4' || strValue.substr(4,1) == '6') {
if (<insert regular expression comparison here>) {
isValid = true;
}
}
}
}

if (strValue.length == 15) {
if (strValue.substr(0,3) == '151') {
if (<insert regular expression comparison here>) {
isValid = true;
}
}
}

if (isValid) {
return true;
} else {
alert(&quot;Invalid entry!&quot;);
return false;
}
}
 

<p>Thanks for your quick response.

<p>scriptOhead:

<p>if the name of my textbox is empnum and name of my form is frm,
should i say:

<p>var lgn = frm.empnum.value.length;alert(lgn);
<p>var txt = frm.empnum.value;

<p>Also I dont think the code above is checking to see if when it is 12 characters and if first digit is 6, then 2nd character must be &quot;d e or f&quot; and the rest any alphanumeric.

<P>Sorry, I am a very new js user.

<p>Thanks a lot.
 
Use

var lgn = document.frm.empnum.value.length;alert(lgn);
var txt = document.frm.empnum.value;

I added the code for the d,e,f bit in a second post.
 

<p>scriptOhead:

<p>some of the logic doesnt work:

<P>if length = 15, we can only have &quot;0123456789abcdefghABCDEFGH&quot;.
but the code is currently accepting a-z

<P>if length = 12, but first character is not 6,
we can only have &quot;0123456789abcdefghABCDEFGH&quot;.
but the code is currently accepting a-z

<P>if length=12 AND first character is 6,
we may have the 5th character 0-9 or a-z,
not just &quot;4&quot; or &quot;6&quot; (we only need the 5th digit being a &quot;4&quot; or &quot;6&quot; if the first number IS NOT 6.
If length is 12 and first character is 6,
all we care about is the 2nd character, which must be d e or f. Everything else (3rd to 12th) could be 0-9 or a-z

<p>Does this make sense, gosh, now I have a headache..

<p>So complex.

<p>Thanks again for all your help guys.

 
OK, this is very close if not correct. Just paste it into a file for testing. Enter test numbers and click anyhwere outside the text box to test it.

<html>
<head>
<title>Form test</title>
<script type = &quot;text/javascript&quot;>

var nonAlphaNumeric = /[^a-zA-Z0-9]/;
var limitedChars = /[^a-hA-H0-9]/;
var specialChars = /[^def]/;

function A(){

var lgn = document.a.b.value.length;
var txt = document.a.b.value;
var nmb = 151;

if(lgn != 12 && lgn != 15){
alert(&quot;Wrong number of chars.&quot;);
return;
}

if(txt.match(nonAlphaNumeric))
{
alert(&quot;Non Alpha Numeric Carachter Entered.&quot;);
return;
}


if(lgn == 12){


if(txt.substr(0, 1) == 6){
var t = txt.substr(1, 1);
if(t.match(specialChars)){
alert(&quot;Incorrect carachter at position 2.&quot;);
return;
}
}
if(txt.substr(0, 1) != 6){
if(txt.match(limitedChars))
{
alert(&quot;Incorrect characters&quot;);
return;
}
if(txt.substr(4, 1) != &quot;4&quot; && txt.substr(4, 1) != &quot;6&quot;){
alert(&quot;Incorrect carachter at position 5.&quot;);
return;
}
}

}


if(lgn == 15){
if(!(txt.substr(0, 3) == nmb)){
alert(&quot;Error in first three carachters&quot;);
return;
}
if(txt.match(limitedChars))
{
alert(&quot;Incorrect characters&quot;);
return;
}
}
alert(&quot;Valid entry.&quot;);
}

</script>


</head>

<body bgcolor=&quot;#ffffff&quot;>



<form name = &quot;a&quot;><input type = &quot;text&quot; name = &quot;b&quot; onblur = &quot;A()&quot;></form>

</body>

</html>
 
scriptOhead:

Thank you so so so very much.

Worked like a charm.

Thanks to everyone who gave this a shot.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top