Jump to content

[SOLVED] Validation Function


ballouta

Recommended Posts

Hello

 

I googled for a function that validates if the username or password fields submitted are between 5 and 12 characters but my form was damaged. I give up.

Please Help.

This is the .js file that I have and it working for the Full Name (fname), and email.

I wanna make sure that the subscriber is writing characters between 4 and 12.

would you please give me a fucntion that i can include in my js file ?

 

function isReady() {

// check if the First Name is valid
    if (isProper(frm.fname.value) == false) {
        alert("Please enter your First Name");
        return false;
    }

// check if the email address is valid
    if (isEmail(frm.email.value) == false) {
        alert("Please enter a valid email address");
        return false;
    }

        // check if the country is selected
    if (isProper(frm.country.value) == false) {
        alert("Please select a country");
        return false;
     }

// check if the User Name is valid
    if (isProper(frm.user.value) == false) {
        alert("Please enter a valid username");
        return false;
     }

// check if the Password is valid
    if (isProper(frm.pass.value) == false) {
        alert("Please enter a correct password");
        return false;
     }


    return true;
    /**********************************************/
}

function isEmail(string)
{
if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;
      
}     

/**********************************************/                 
function isProper(string) {
  if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

Link to comment
https://forums.phpfreaks.com/topic/111481-solved-validation-function/
Share on other sites

You can use a function like this:

<script language="Javascript">
function validate(text)
{
if (text.length > 3 && text.length < 13)
	return true;
else
	return false;
}
</script>

 

You can either change the return code to do what you want it to do or call it like:

if (validate(textbox1.value)) alert("valid");

BY the way, I believe there is a problem with the isProper function you posted above:

function isProper(string) {
  if (string.search(/^\w+( \w+)?$/) != -1)
        return true;
    else
        return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

The first IF/ELSE statemetn will either return true or false, so everything after that is never run. This should fix it if I am reading it correctly:

function isProper(string) {
   if (string.search(/^\w+( \w+)?$/) == -1) return false;
        
   if (!string) return false;
   
   var iChars = "*|,\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         return false;
   }
   return true;

}

 

Also, you could replace the for loop with a single regex pattern.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.