/    Sign up×
Community /Pin to ProfileBookmark

text box must be 10 digits .. else..

Hi All,

I’m having a hard time in getting the last part of this javascript validation.

Basically, if the text box is not 10 digits, throw an alert…

if (document.myform.Global_ID.value==””)
{
alert(“Please enter your Global ID.”)
document.myform.Global_ID.focus();
return false;
}
else if (document.myform.Global_ID.value.charAt(0) ==” ” )
{
alert(“Global ID code cannot begin with a blank space.”)
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
return false;
}

else
{
var Global_ID = document.myform.Global_ID.value;
var cod = parseFloat(Global_ID)
if ((Global_ID!=”+cod)&&(Global_ID!=’0’+cod)&&(Global_ID!=’00’+cod))
{
alert(“Global ID may only contain numbers.”)
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
return false;
}
}

else
{
var ID_Length = document.myform.Global_ID.length;
if ((ID_Length < 10) || (ID_Length > 10))
{
alert(“Global ID must contain 10 digits.”)
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
return false;
}
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@hoangkcJul 23.2009 — to validate a textbox with 10 digits, may be you should want to use regexp
<i>
</i>var r = /^d{10}$/;
if(r.test(document.myform.Global_ID.value)) alert("ok");
else alert("globalID must be 10 digits!");
Copy linkTweet thisAlerts:
@llckllauthorJul 23.2009 — I put it in like this:

if (document.myform.Global_ID.value=="")
{
alert("Please enter your Global ID.")
document.myform.Global_ID.focus();
return false;
}
else if (document.myform.Global_ID.value.charAt(0) ==" " )
{
alert("Global ID code cannot begin with a blank space.")
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
return false;
}

else if
{
var Global_ID = document.myform.Global_ID.value;
var cod = parseFloat(Global_ID)
if ((Global_ID!=''+cod)&&(Global_ID!='0'+cod)&&(Global_ID!='00'+cod))
{
alert("Global ID may only contain numbers.")
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
return false;
}
}

else
{
var r = /^d{10}$/;

if(r.test(document.myform.Global_ID.value)) alert("ok");

else alert("globalID must be 10 digits!");

}

But it doesn't seem t work still.
Copy linkTweet thisAlerts:
@hoangkcJul 23.2009 — You can create a new blank html file, paste those code below, and let me know if it works.
<i>
</i>&lt;input type="text" id="txt"&gt; &lt;input type="button" value="check" onclick="check()"&gt;
&lt;script&gt;
function check()
{
var r = /^d{10}$/;
if(r.test(document.getElementById("txt").value)) alert("ok");
else alert("globalID must be 10 digits!");
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@llckllauthorJul 23.2009 — <html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript">

// validation form starts here

// Declaring required variables

var digits = "0123456789";

// non-digit characters which are allowed in phone numbers

var phoneNumberDelimiters = "()- .";

// characters which are allowed in international phone numbers

// (a leading + is OK)

var validWorldPhoneChars = phoneNumberDelimiters + "+";

// Minimum no of digits in an international phone no.

var minDigitsInIPhoneNumber = 1;

function valid()

{

if (document.myform.First_Name.value=="")
{
alert("Please enter your first name.")
document.myform.First_Name.focus()
document.myform.First_Name.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}
else if (document.myform.First_Name.value.charAt(0)==" " )
{
alert("First name cannot begin with a blank space.")
document.myform.First_Name.focus()
document.myform.First_Name.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}

if (document.myform.Last_Name.value=="")
{
alert("Please enter your last name.")
document.myform.Last_Name.focus()
document.myform.Last_Name.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}
else if (document.myform.Last_Name.value.charAt(0) ==" " )
{
alert("Last name cannot begin with a blank space.")
document.myform.Last_Name.focus()
document.myform.Last_Name.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}

//Starts EMAIL validation
if (document.myform.EMAIL.value=="")
{
alert("Please enter your e-mail address.")
document.myform.EMAIL.focus()
document.myform.EMAIL.select();
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}
else if (document.myform.EMAIL.value.charAt(0)==" " )
{
alert("E-mail address cannot begin with a blank space.")
document.myform.EMAIL.focus()
document.myform.EMAIL.select()
return false;
}
var strEmail=document.myform.EMAIL.value;
strEmail= strEmail.toLowerCase();
if ((strEmail.indexOf("@")==-1)||(strEmail.indexOf(".")==-1))
{ return error()}
else
{
var word=strEmail.split("@");
if(word.length >2) {return error()}
if(word[0]=="")
{return error()}
word1= word[1];
if(word[1].indexOf(".")==-1)
{return error()}
var sWord=word1.split(".");
if(sWord[1].length <2)
{return error()}
}
if(strEmail.indexOf("..") >= 0){return error()}
var strDot=strEmail.split("@");
if (strDot[1].indexOf(".")==0)
{
return error()
}


var valLet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@_-0123456789"
for (i=0;i<=strEmail.length;i++)
{
flgfound = false;
for(k=0;k<=valLet.length;k++)
{
if(strEmail.charAt(i) == valLet.charAt(k))
{
flgfound = true;
break;
}

}
if(flgfound == false)
{
alert ("E-mmail addresses can contain letters, numbers and '_' only.");
document.myform.EMAIL.focus();
document.myform.EMAIL.select();
return false;
}
}
j= strEmail.length-1
ch=strEmail.charCodeAt(j);
if (((ch<45)||(ch>122))||((ch<90)||(ch<94)))
{
alert ("E-mail addresses should end with letters only.");
document.myform.EMAIL.focus();
document.myform.EMAIL.select();
return false;
}


// end of Email validation

if (document.myform.Global_ID.value=="")
{
alert("Please enter your Global ID.")
document.myform.Global_ID.focus();
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}
else if (document.myform.Global_ID.value.charAt(0) ==" " )
{
alert("Global ID code cannot begin with a blank space.")
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}

else
{
var Global_ID = document.myform.Global_ID.value;
var cod = parseFloat(Global_ID)
if ((Global_ID!=''+cod)&&(Global_ID!='0'+cod)&&(Global_ID!='00'+cod))
{
alert("Global ID may only contain numbers.")
document.myform.Global_ID.focus()
document.myform.Global_ID.select()
mCal.hide();
mCal1.hide();
mCal2.hide();
return false;
}
}


if (document.myform.Business.selectedIndex==0)
{
alert("Please select your business.")
document.myform.Business.focus()

mCal.hide();

mCal1.hide();

mCal2.hide();

return false;

}



if (document.myform.Program.selectedIndex==0)
{
alert("Please select your Program.")
document.myform.Program.focus()

mCal.hide();

mCal1.hide();

mCal2.hide();

return false;

}

if (document.myform.Program_Date.value=="")
{
alert("Please select your Program Date.")
document.myform.Program_Date.focus()
document.myform.Program_Date.select()
mCal1.hide();
mCal2.hide();
return false;
}

if (document.myform.Arrival_Date.value=="")
{
alert("Please select your Arrival Date.")
document.myform.Arrival_Date.focus()
document.myform.Arrival_Date.select()
mCal.hide();
mCal2.hide();

return false;
}

if (document.myform.Departure_Date.value=="")
{
alert("Please select your Departure Date.")
document.myform.Departure_Date.focus()
document.myform.Departure_Date.select()
mCal.hide();
mCal1.hide();
return false;
}


if (document.myform.acceptTerms.checked==false)
{
alert("If you would like to submit information, please check acceptance of privacy Statement terms.")
document.myform.acceptTerms.focus();
document.myform.acceptTerms.select();
return false;
}



}


function error()

{

alert("Please enter a valid e-mail address.")

document.myform.EMAIL.focus();

document.myform.EMAIL.select();

return false;

}

function isInteger(s)

{ var i;

for (i = 0; i < s.length; i++)

{


// Check that current character is number.

var c = s.charAt(i);

if (((c < "0") || (c > "9"))) return false;

}

// All characters are numbers.

return true;

}

function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if(i > 0)
if (s.charAt(i) == "+")
return false
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



function textCounter(field, countfield, maxlimit) {

if (field.value.length > maxlimit) // if story is too long

field.value = field.value.substring(0, maxlimit); // trim characters

else countfield.value = maxlimit - field.value.length; // else, update character counter

}// End -->

</script>

<link rel="stylesheet" type="text/css" href="css/dhtmlxcalendar.css">

<script src="js/dhtmlxcommon.js" defer="defer"></script>

<script src="js/dhtmlxcalendar.js" defer="defer"></script>

<script defer="defer">window.dhx_globalImgPath="imgs/";</script>

</head>
×

Success!

Help @llckll spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...