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!

IsInteger problem with form validate 2

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
Hey all ive got the following form vadiation
Code:
<script type="text/javascript">
<!--
function formValidation(form){
if(notEmpty(enrollform.csrname)){
if(notEmpty(enrollform.LastName_Dem)){
if(notEmpty(enrollform.FirstName_Dem)){
if(notEmpty(enrollform.SSN_Dem)){
if(notEmpty(enrollform.DOB_Dem)){
if(notEmpty(enrollform.Gender_Dem)){
if(notEmpty(enrollform.StAdd_Dem)){
if(notEmpty(enrollform.City_Dem)){
if(notEmpty(enrollform.State_Dem)){
if(notEmpty(enrollform.Zip_Dem)){
if(notEmpty(enrollform.DPN_Dem)){
if(notEmpty(enrollform.Email_Dem)){
if(notEmpty(enrollform.PW_Dem)){
if(notEmpty(enrollform.PWRM_Dem)){
if(notEmpty(enrollform.Agency_Dem)){
if(notEmpty(enrollform._date)){
if(isInteger(enrollform.SSN_Dem)){
return true;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return false;
}
function notEmpty(elem){
var str = elem.value;
if(str.length == 0){
alert("You must fill in all required fields (*)");
return false;
} else {
return true;
}
}
//-->
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
        return alert("SSN must contain all numbers");
        return false;
    }
   
 
 return true;
}

</script>


I think my problem lies right here
Code:
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
        return alert("SSN must contain all numbers");
        return false;
    }
   
 
 return true;
}

The purpose of that was to make sure field is all numbers. But it seems to just pass it either way.

Can anyone be so kind as to help me out?

 
There is always room to improve. The idea is this.
[tt]function isInteger(s){
if (/^[+|-]{0,1}[0-9]+\.{0,1}0*$/.test(s)) {
return true;
} else {
//any alert here
return false;
}
}[/tt]
 
Thanks for the response i changed my function to the way that you posted but it still gives me my alert whether there are numbers or not? Is there a problem with the way i call it?
 
Here's the updated code
Code:
<script type="text/javascript">
<!--
function formValidation(form){
if(notEmpty(enrollform.csrname)){
if(notEmpty(enrollform.LastName_Dem)){
if(notEmpty(enrollform.FirstName_Dem)){
if(notEmpty(enrollform.SSN_Dem)){
if(notEmpty(enrollform.DOB_Dem)){
if(notEmpty(enrollform.Gender_Dem)){
if(notEmpty(enrollform.StAdd_Dem)){
if(notEmpty(enrollform.City_Dem)){
if(notEmpty(enrollform.State_Dem)){
if(notEmpty(enrollform.Zip_Dem)){
if(notEmpty(enrollform.DPN_Dem)){
if(notEmpty(enrollform.Email_Dem)){
if(notEmpty(enrollform.PW_Dem)){
if(notEmpty(enrollform.PWRM_Dem)){
if(notEmpty(enrollform.Agency_Dem)){
if(notEmpty(enrollform._date)){
if(isInteger(enrollform.SSN_Dem)){
return true;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return false;
}
function notEmpty(elem){
var str = elem.value;
if(str.length == 0){
alert("You must fill in all required fields (*)");
return false;
} else {
return true;
}
}
//-->
function isInteger(s){
 if(/^[+|-]{0,1}[0-9]+\.{0,1}0*$/.test(s)) {
  return true;
   } else {
alert("SSN must be all numbers");  
return false;    }
}
Thanks again
 
Thanks for the response. I seem to get the error "object expected" on this line this line
isInteger(enrollform.SSN_Dem.value)
thanks for your help im on top of my asp skills but just a little in the dark on some javascript :)

<% ASP Web App Development %>
 
There are something wrong with what you pass to the function. Should not look miles away... always look at the s. In the function at the alert part, alert this.
[tt]alert(s+"\n"+escape(s));[/tt]
There should be some nonprintable characters there. Though my function is not perfect, it is capable at least to filter correctly for common number!
 
I see now I had adjusted a } to the wrong place to get the object expected and the alert(s+"\n"+escape(s)); gave me object until i switched back to .value

Thank you both very much Its going great

<% ASP Web App Development %>
 
Upon re-reading what I posted, I had a mistake in the function. A spurious "|" is inserted there what should not be (I was thinking about "or" which is not needed.) Hence a relist here.
[tt]
function isInteger(s){
if (/^[[highlight]+-[/highlight]]{0,1}[0-9]+\.{0,1}0*$/.test(s)) {
return true;
} else {
//any alert here
return false;
}
}
[/tt]
Hence werD420, if you use it or expand on it, I would appreciate if you can make the corresponding correction!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top