/    Sign up×
Community /Pin to ProfileBookmark

alert functions not working correctly

hi,

For some reason the alert functions in my code seem to be not working correctly and i cant work out why… The first two work but the remaining ones dont. why is this??? Have a got some syntax wrong here??

[code]
var temp = document.callmeback_form.telephone.value;

if (temp==””)
{
alert(“Enter a valid UK Phone number including area code”);
document.callmeback_form.telephone.focus();
document.callmeback_form.telephone.style.background = ‘#FF8600’;
document.callmeback_form.telephone.style.border = “1px solid #A5ACB2”;
document.callmeback_form.telephone.style.width = “132px”;
document.callmeback_form.telephone.style.height = “15px”;
return false;
}

if (!temp.match(/^(+44[0-9]{10}|0044[0-9]{10}|0[0-9]{10})$/) )
{

if (temp.match(/^(+44)$/) && temp.length < 13)
{
alert(“The number you have entered with this prefix (+44) is too short, please check and try again”);
return false;
}

if (temp.match(/^(+44)$/) && temp.length > 13)
{
alert(“The number you have entered with this prefix (+44) is too long, please check and try again”);
return false;
}

if (temp.match(/^(044)$/) && temp.length < 14)
{
alert(“The number you have entered with this prefix (0044) is too short, please check and try again”);
return false;
}
if (temp.match(/^(044)$/) && temp.length > 14)
{
alert(“The number you have entered with this prefix (0044) is too long, please check and try again”);
return false;
}

document.callmeback_form.telephone.focus();
document.callmeback_form.telephone.style.background = ‘#FF8600’;
document.callmeback_form.telephone.style.border = “1px solid #A5ACB2”;
document.callmeback_form.telephone.style.width = “132px”;
document.callmeback_form.telephone.style.height = “15px”;
return false;
}

[/code]

?
Thanks in advance. Dan

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 26.2007 —  if (temp.match(/^+44/) &amp;&amp; temp.length &lt; 13)
{
alert("The number you have entered with this prefix (+44) is too short, please check and try again");
return false;
}

<i> </i> if (temp.match(/^+44/) &amp;&amp; temp.length &gt; 13)
<i> </i> {
<i> </i> alert("The number you have entered with this prefix (+44) is too long, please check and try again");
<i> </i> return false;
<i> </i> }

<i> </i> if (temp.match(/^0044/) &amp;&amp; temp.length &lt; 14)
<i> </i> {
<i> </i> alert("The number you have entered with this prefix (0044) is too short, please check and try again");
<i> </i> return false;
<i> </i> }
<i> </i> if (temp.match(/^0044/) &amp;&amp; temp.length &gt; 14)
<i> </i> {
<i> </i> alert("The number you have entered with this prefix (0044) is too long, please check and try again");
<i> </i> return false;
<i> </i> }
Copy linkTweet thisAlerts:
@dan_coop3rauthorOct 26.2007 — Thanks Fang!!!
Copy linkTweet thisAlerts:
@Mr_JOct 26.2007 — Fang beat me ?
Copy linkTweet thisAlerts:
@dan_coop3rauthorOct 26.2007 — hmm... here is something i havnt tried to do yet.

If a user was to enter lets say +441327657345 that would be a correct phone number, but they may enter +4401327657345 and this would be incorrect as if you have the +44 the 0 is not required and the code created previously would raise the "this number is too long" alert.

How would I go about stripping the 4th character entered into the field if it is a 0 and the string begins with +44???
<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--

function validate_form ( )
{

<i> </i>if (document.callmeback_form.name.value == "" )
<i> </i>{
<i> </i>alert ( "Please enter your name." );
<i> </i>document.callmeback_form.name.focus();
<i> </i>document.callmeback_form.name.style.background = '#FF8600';
<i> </i>document.callmeback_form.name.style.border= "1px solid #A5ACB2";
<i> </i>document.callmeback_form.name.style.width= "132px";
<i> </i>document.callmeback_form.name.style.height= "15px";
<i> </i>return false;
<i> </i>}

var temp = document.callmeback_form.telephone.value;

<i> </i> if (temp=="")
<i> </i> {
<i> </i> alert("Enter a valid UK Phone number including area code");
<i> </i>document.callmeback_form.telephone.focus();
<i> </i>document.callmeback_form.telephone.style.background = '#FF8600';
<i> </i>document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
<i> </i>document.callmeback_form.telephone.style.width = "132px";
<i> </i>document.callmeback_form.telephone.style.height = "15px";
<i> </i>return false;
<i> </i>}

<i> </i>if (!temp.match(/^(+44[0-9]{10})$/) )
<i> </i>{

<i> </i> if (temp.match(/^+44/) &amp;&amp; temp.length &gt; 13)
<i> </i> {
<i> </i> alert("I have stripped the 3rd character if it is a 0");
<i> </i> document.callmeback_form.telephone.focus();
<i> </i> document.callmeback_form.telephone.style.background = '#FF8600';
<i> </i> document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
<i> </i> document.callmeback_form.telephone.style.width = "132px";
<i> </i> document.callmeback_form.telephone.style.height = "15px";
<i> </i> return false;
<i> </i> }

<i> </i>alert("Enter a valid UK Phone number including area code");
<i> </i>return false;

<i> </i>}
}
//--&gt;
&lt;/script&gt;

Thanks in advance. Dan?
Copy linkTweet thisAlerts:
@dan_coop3rauthorOct 26.2007 — looks like iver got it now... using the replace command now.?
Copy linkTweet thisAlerts:
@dan_coop3rauthorOct 26.2007 — Am I using this replace command incorrectly??? ?
<i>
</i>&lt;script type="text/javascript"&gt;
&lt;!--

function validate_form ( )
{

<i> </i>if (document.callmeback_form.name.value == "" )
<i> </i>{
<i> </i>alert ( "Please enter your name." );
<i> </i>document.callmeback_form.name.focus();
<i> </i>document.callmeback_form.name.style.background = '#FF8600';
<i> </i>document.callmeback_form.name.style.border= "1px solid #A5ACB2";
<i> </i>document.callmeback_form.name.style.width= "132px";
<i> </i>document.callmeback_form.name.style.height= "15px";
<i> </i>return false;
<i> </i>}

<i> </i>var temp = document.callmeback_form.telephone.value;

<i> </i> if (temp=="")
<i> </i> {
<i> </i> alert("Enter a valid UK Phone number including area code");
<i> </i>document.callmeback_form.telephone.focus();
<i> </i>document.callmeback_form.telephone.style.background = '#FF8600';
<i> </i>document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
<i> </i>document.callmeback_form.telephone.style.width = "132px";
<i> </i>document.callmeback_form.telephone.style.height = "15px";
<i> </i>return false;
<i> </i>}

<i> </i>if (!temp.match(/^(+440[0-9]{10})$/) )
<i> </i>{
<i> </i> if (temp.match(/^+440/) &amp;&amp; temp.length ==14)
<i> </i> {
<i> </i> temp = temp.replace('+440','+44');
<i> </i> alert("I have stripped the 3rd character if it is a 0");
<i> </i> return false;
<i> </i> }



<i> </i>alert("Enter a valid UK Phone number including area code");
<i> </i>return false;

<i> </i>}
}
//--&gt;
&lt;/script&gt;


Obviously once it is working I will take out the alert and replace the return false with a return true.

Thanks in advance for any help I can get. Dan
Copy linkTweet thisAlerts:
@FangOct 26.2007 — replace is correct, but the if loops with match are incorrect.

The inner loop will not be reached and is redundant anyway.
if (temp.match(/^+440[0-9]{10}$/) )
{
temp = temp.replace('+440','+44');
alert("I have stripped the 3rd character if it is a 0");
return true;
}

<i> </i>alert("Enter a valid UK Phone number including area code");
<i> </i>return false;

}
×

Success!

Help @dan_coop3r 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.27,
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,
)...