Jump to content

Registration Validation...

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Guest_GerarD_91_*

Guest_GerarD_91_*
  • Guests
hi there ppl. I'm doing a registration form now, and need to validate the fields in there...
I'm using some client-side validation first (js), but need to use a server-side validation too. Found this cool js code, and just wanted to know if someone can tell me how to do those functions in php.


var letters=' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ'

var numbers='1234567890'

var signs='_-#"/.,°'


function alpha(e,allow) {

     var k;

     k=document.all?parseInt(e.keyCode): parseInt(e.which);

     return (allow.indexOf(String.fromCharCode(k))!=-1);

}


i use it with a keypress event in my textbox, so i can only write the characters i want... to implement the function i just type this:

onkeypress="return alpha(event,letters)";

could use letters+numbers.. and so on.

I don't want the php script to go with the keypress, just need a function where i can config the chars i want, and after the submit, rows false or true..

well that's all for now... :)

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You may want to take a look at regular expressions. They can be used in A LOT of programming languages, including javascript (see example below) and php ( preg_match(pattern, target) )


<html> 

<head> 

</head> 


<script type="text/javascript"> 

var result;

var input;


function init(){

	result = document.getElementById('result');

	input = document.getElementById('input');

}


function validateLettersOnly(){   

	var regex = /^[ ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ]+$/;

	test(regex);

} 


function validateNumbersOnly(){   

	var regex = /^[1234567890]+$/;

	test(regex);

} 


function validateNumbersOnly2(){   

	var regex = /^\d+$/;

	test(regex);

}


function validateSpecialCharacters(){

	var regex = /^[_\-#"\/\.,°]+$/

	test(regex);

}


function test(regex){

	if( regex.test(input.value) ){

		result.innerHTML = "Correct";

	} else {

		result.innerHTML = "Incorrect";

	}

}


</script> 


<body onload="init()"> 

	

		<input id="input" type="text"/>

		<div id="result"></div>

		<input type="button" value="validateLettersOnly" onclick="validateLettersOnly()"/>

		<input type="button" value="validateNumbersOnly" onclick="validateNumbersOnly()"/>

		<input type="button" value="validateNumbersOnly2" onclick="validateNumbersOnly2()"/>

		<input type="button" value="validateSpecialCharacters" onclick="validateSpecialCharacters()"/>


</body> 

</html>  


In javascrpit you define a regex between 2 forward slashes /pattern/
The pattern of /^[1234567890]+$/
is actually ^[1234567890]+$
^ at the beginning and $ at the end mean the whole line must match this regex.
[1234567890] = Any character within these brackets can be used
'+' = 1 or more times. (so empty string is invalid).

"\d" stands for any digit in regexes.

Certain special characters must be escaped in regexes, like the '-' and '/'. This is done by prepending a backwards slash.
So
_-#"/\.,°
became
_\-#"\/\.,°

The '/' because otherwise javascript thinks the regex is finished.
The '-' because that's a symbol used for range. If you wanted to test a value if it contains letters from the alphabet you would do (normal alphabet, not the spanish/italian/portugese you use :) )
^[a-ZA-Z]+$

(A quick google for "regex" or "regular expression" propably gives you all the info you need)

#3
Guest_GerarD_91_*

Guest_GerarD_91_*
  • Guests
So, i'm reading about the preg_match function... And, i think i can use the same regular expressions i have in my js to build the function i need... But, how can i do, like a dynamic preg_mathc.... cuz' i see ur example, and u have a function for every type of char, but i want to use just one function that create the expression by passing the vars with the parameters... get it.?..

function alpha(e,allow) { 

     var k; 

     k=document.all?parseInt(e.keyCode): parseInt(e.which); 

     return (allow.indexOf(String.fromCharCode(k))!=-1); 

}  

that create the expression,, so i just have to add the chars i want by passing them.... That way, if i want to validate a Name, i pass a var with the Letters,,, and if i want to validate a Username, i pass a var with Letters, Numbers and some special chars...

#4
bmett

bmett

    Newbie

  • Members
  • PipPip
  • 12 posts
We use the following script to do form validation. I can recommended it. It's very easy to get going and also very easy to customise. It's based on jQuery.

Form Validation

Cheers,
Bjorn

#5
Guest_GerarD_91_*

Guest_GerarD_91_*
  • Guests
Thanks @bmett, that's some cool script, and very easy to work with... I'll be using it to on my form, jQuery makes life so easy xDDD...

#6
bmett

bmett

    Newbie

  • Members
  • PipPip
  • 12 posts
Glad I could help.

The script is good. We customised it quite a bit and added custom checks for many different validations.

And yes, jQuery is awesome :thumbup:

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Although the function is a bit silly, you can still do it in PHP like this..

$letters=' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ';
$numbers='1234567890';
$signs='_-#"/.,°';

function filterstring($input, $allowed) {
     return preg_replace("/[^" . preg_quote($allowed) . "]/", "", $input);
}

//abcd1234
echo filterstring("abcd*()_-1234", $letters . $numbers);

There are a few ways to define your letters in regular expressions, i.e. [:alpha:] or better matching a Unicode set ("\p{Latin}" is supported by PCRE for example) although this may be harder to add upon, so I left it as be.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users