TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-15-2007, 12:15 PM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Form validation with Regex [Prototype]

How to validate forms, through regex, with javascript (if framework, prototype)?
Haris is offline  
Reply With Quote
Old 11-15-2007, 01:31 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

That's rather vague Haris, could you be a bit more specific? Are you trying to validate an email address or something?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-15-2007, 07:52 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

I'm trying to validate a URL. The most convenient options looks like regex. After a little bit of research, I found Formassembly w3forms convenient javascript validation script.

It supports a bit of validation functions itself ad it supports validating data through regex.

I would like to ask that with javascript validation, should I also use server-side validation (I.E with PHP) since someone can easily modify js through firefox.
Haris is offline  
Reply With Quote
Old 11-15-2007, 08:01 PM   #4 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

you always use server side validation.

Always always always.

JavaScript validation is only a convenient way to the user to fix their problems before they have to submit. But ultimately, it doesn't count for anything legit.

Also, a lot of bots won't even pick up the javascript, so someone could just as easily spam your site with false information.

Last edited by dschreck : 11-15-2007 at 08:02 PM. Reason: typo
dschreck is offline  
Reply With Quote
Old 12-06-2007, 03:05 AM   #5 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

i use this version: (PHP & Javascript)

http://www.roscripts.com/Ajax_form_validation-152.html
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 12-06-2007, 04:44 AM   #6 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

I dont think im the best qualified for giving this solution but i have created this pattern and although it is not perfect as it is It may give you a starting point for what you are trying to achieve.

Validate URL:

/^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name |co|me)(\.[a-z]{1,3})?$/i

Example:

PHP Code:
<?php 
$szString 
"http://www.talkPHP.com";
if (
preg_match('/^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name|co|me)(\.[a-z]{1,3})?$/i'$szString))
    echo 
"This is a valid URL";
?>
__________________
http://www.mattvarone.com

Last edited by Matt83 : 12-06-2007 at 09:19 AM.
Matt83 is offline  
Reply With Quote
Old 12-07-2007, 02:38 PM   #7 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

I been playing a bit with prototype and thought of giving this a try, im sure it can be improved but anyways here it is, i hope it helps:

HTML Code:
	<script src="prototype.js" type="text/javascript"></script>
	<script type="text/javascript">
	function validateURL (szURL,szTarget,szForm) 
	{
		var bValid = /^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name|co|me)(\.[a-z]{1,3})?$/.test(szURL);
		if (bValid == false) {
			$(szTarget).innerHTML = "Please enter a valid URL.";
		} else {
			$(szTarget).innerHTML = "Valid URL.";
			// $(szForm).submit();
 		}
	};
	</script>
<h1>Validate URL</h1>
<form id="myForm" action="" method="post">
	<p><label>Url:</label><br/>
	<input type="text" name="url" value="" id="url" size="40"/></p>
	<div id="info"></div>
	<p><input type="button" value="Send URL &rarr;"  onclick="validateURL($('url').value,'info','myForm')"/></p>
</form>
__________________
http://www.mattvarone.com
Matt83 is offline  
Reply With Quote
Old 12-08-2007, 10:49 AM   #8 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Matt83 View Post
I been playing a bit with prototype and thought of giving this a try, im sure it can be improved but anyways here it is, i hope it helps:

HTML Code:
	<script src="prototype.js" type="text/javascript"></script>
	<script type="text/javascript">
	function validateURL (szURL,szTarget,szForm) 
	{
		var bValid = /^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name|co|me)(\.[a-z]{1,3})?$/.test(szURL);
		if (bValid == false) {
			$(szTarget).innerHTML = "Please enter a valid URL.";
		} else {
			$(szTarget).innerHTML = "Valid URL.";
			// $(szForm).submit();
 		}
	};
	</script>
<h1>Validate URL</h1>
<form id="myForm" action="" method="post">
	<p><label>Url:</label><br/>
	<input type="text" name="url" value="" id="url" size="40"/></p>
	<div id="info"></div>
	<p><input type="button" value="Send URL &rarr;"  onclick="validateURL($('url').value,'info','myForm')"/></p>
</form>
There's a cleaner premade solution available.

Please consider FormAssembly's wForms javascript extension.
Haris is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design