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

Number validation (regular expression) 3

Status
Not open for further replies.

perryair

IS-IT--Management
Apr 7, 2005
91
IL
I am trying to validate numeric characters but with no success, actually what I would need to validate is:

min characters (9) max characters (11)
the first character has to be (0) and the rest(0123456789)

see my code below..

if(myValidation.Phone.value.length == 0)
{
alert("Phone is empty");
myValidation.Phone.focus();
return false;
}

var str=myValidation.phone.value
var filter=/^\0\d{9,11}(0123456789)$/i
if (filter.test(str))
{
return true;
}
else
{
alert("phone code please!!");
myValidation.phone.focus();
return false;
}
 
I am no expert. My feeble reply is practice, but I think the regexp for that task is as follows.

If it fails, I am sorry in advance ;)

/(0{1})\d{8,10}/

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
The leading zero is included in the 9-11 character range, right?

var filter=/^0\d{8,10}$/

Adam
 
Thanks... however, now it is not jump to next statement (eMail).

Any idea?


if(myValidation.Phone.value.length == 0)
{
alert("Phone is empty");
myValidation.Phone.focus();
return false;
}

var strphone=myValidation.Phone.value
var phonechar=/^0\d{8,10}$/i
if (phonechar.test(strphone))
{
return true;
}
else
{
alert("phone code please!!");
myValidation.Phone.focus();
return false;
}


if(myValidation.eMail.value.length == 0)
{
alert("Please enter email address");
myValidation.eMail.focus();
return false;
}


var stremail=myValidation.eMail.value
var emailchar=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (emailchar.test(stremail))
{
return true;
}
else
{
alert("Invalid email address");
myValidation.eMail.focus();
return false;
}
}
 
If all that code is encapsulated in one function then the reason is because you are returning true if it passes your phone regexp check. Any time you return something from a function the function will cease to perform any more code past that point.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Yeah, what kaht said.
Code:
if(myValidation.Phone.value.length == 0)
{
alert("Phone is empty");
myValidation.Phone.focus();
return false;
}

var strphone=myValidation.Phone.value
var phonechar=/^0\d{8,10}$/i
if (!phonechar.test(strphone))
{
alert("phone code please!!");
myValidation.Phone.focus();
return false;
}


if(myValidation.eMail.value.length == 0)
{
alert("Please enter email address");
myValidation.eMail.focus();
return false;
}


var stremail=myValidation.eMail.value
var emailchar=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (!emailchar.test(stremail))
{
alert("Invalid email address");
myValidation.eMail.focus();
return false;
}

return true;

Adam
 
Doesn't make sense since now it skip the "var" statements.
Any idea how to resolve this?

Thanks.
 
Still doesn't work, do you have an example? may be I am wrong..
 
Perhaps you could post your complete function instead of posting 1 or 2 line replies, we're simply stabbing in the dark at this point if it's not working for you because adam's solution was spot on for the "function ending pre-maturely" question.

Also, learn to indent your code, it makes it 9999999999999 times easier to read. There's a reason your teacher in CS101 deducted points from your coding assignments for not indenting - it's incredibly poor coding practice.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Here you go..... the question is how I can move over to last statements (eMail)... I tried to put the "var" on top as Adam said and I couldn't see any difference.


return false;
}

if(myValidation.LastName.value.length == 0)
{
alert("Please enter last name");
myValidation.LastName.focus();
return false;
}


if(myValidation.Phone.value.length == 0)
{
alert("Please enter phone number");
myValidation.Phone.focus();
return false;
}

var strphone=myValidation.Phone.value
var phonechar=/^0\d{8,10}$/i
if (phonechar.test(strphone))
{
return true;
}
else
{
alert("Invalid phone number");
myValidation.Phone.focus();
return false;
}



if(myValidation.eMail.value.length == 0)
{
alert("Please enter email address");
myValidation.eMail.focus();
return false;
}
var stremail=myValidation.eMail.value
var emailchar=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (emailchar.test(stremail))
{
return true;
}
else
{
alert("Invalid email address");
myValidation.eMail.focus();
return false;
}
}
 
The advice repeatedly given is: when it is a valid thing, do nothing, and continue to validate the next entries. Interrupt the chain only when the first invalid entry is identified.
[tt]
return false;
}

if(myValidation.LastName.value.length == 0)
{
alert("Please enter last name");
myValidation.LastName.focus();
return false;
}


if(myValidation.Phone.value.length == 0)
{
alert("Please enter phone number");
myValidation.Phone.focus();
return false;
}

var strphone=myValidation.Phone.value
var phonechar=/^0\d{8,10}$/i
if (phonechar.test(strphone))
{
[red]//[/red]return true; //need to do nothing more
}
else
{
alert("Invalid phone number");
myValidation.Phone.focus();
return false;
}

if(myValidation.eMail.value.length == 0)
{
alert("Please enter email address");
myValidation.eMail.focus();
return false;
}
var stremail=myValidation.eMail.value
var emailchar=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (emailchar.test(stremail))
{
[red]//[/red]return true; //same meaning
}
else
{
alert("Invalid email address");
myValidation.eMail.focus();
return false;
}
[red]return true;[/red] //arrive that far, things are all well
}
[/tt]
 
I'm just going to reiterate on what tsuji said because it's exactly what adam and I have already posted, however since you've pasted your code back in without the changes we've suggested, then we probably weren't explicit in what we were trying to say.

Here's a small script you can test so that you can understand better:
Code:
<script language=javascript>
function blah() {
   alert("1");
   return true;
   alert("2");
}
window.onload=blah;
</script>
When you paste that code into a new page and run it you will never see the alert box with the number 2. After the alert("1") command is executed, the function returns true. At this point, the function will stop executing any more code, therefore the 2nd alert is not executed. This is the same problem in your function that I'll post here:
Code:
var strphone=myValidation.Phone.value
var phonechar=/^0\d{8,10}$/i
if (phonechar.test(strphone))
{
[b]return true;[/b]
}
else
{
alert("Invalid phone number");
myValidation.Phone.focus();
[b]return false;[/b]
}
Now if you see by the 2 lines that I've bolded that you have a return true and a return false. These are the 2 statements that are ran depending on the if statement above them. However, since you have a return in both cases of the if statement, the function will always return once it hits that if clause.

Please go up and reread all the posts, this was explained in explicit detail above by adam and myself (and tsuji after your latest post). If we are going to take the time to help offer a solution for you, please have the decency to at least try them out (adam's post above removed the return true statements, and then you claimed it didn't work even though your latest posting of your function shows the return true statements still exist, which implies you didn't even try adam's suggestion)

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Gotcha!! thank you very much for your help...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top