Solved

Javascript Disable URL Form

Posted on 2007-04-10
7
795 Views
Last Modified: 2008-02-01
I would like to disable people from inserting links into my form. Are there any tutorials out there for this?
0
Comment
Question by:Nitestarz
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
mzalfres earned 25 total points
ID: 18881813
Hi,

I'd search for regular expression defining any/specific URL and then create script running with 'onSubmit' event, which will check the form against this.

Something like in this example:

http://snippets.dzone.com/posts/show/452

If 'is_URL' function returns true, issue alert/error/whatever else and stop submitting form.

Regards,

Marek ZJ.
0
 
LVL 19

Expert Comment

by:v2Media
ID: 18882023
Javascript will not do it for you; it's too easily disabled. If you want to forbid links in your form data, then you have to do it server-side. Does your server support asp or php or any other server-scripting language?
0
 

Author Comment

by:Nitestarz
ID: 18882085
Yes, PHP is used to process the page.
0
 
LVL 19

Assisted Solution

by:v2Media
v2Media earned 25 total points
ID: 18882570
ok, lets say you want to show an error message to alert the user that links are not permitted. In the form processing portion of your script, include something along these lines:-

Assume $_POST['Submit'] is your form's submit button.

<?php
if(isset($_POST['Submit'])) {

      // reset onpage errors
      $onpageErrors = ''
      
      // concatenate POST vars to $strHaystack
      $strHaystack = '';
      foreach ($_POST as $index => $value){
      
            $strHaystack .= $value;
            
            }
      
      //detect link spammer
      $no_links = false;      
      $needles = '';
      
      //link needles
      $needles[] = '<a href';
      $needles[] = '=http:';
      $needles[] = '="http:';
      $needles[] = '=mailto:';
      
      foreach ($needles as $k => $v){
      
                  if( stristr($strHaystack,$v ) ){
                  $onpageErrors .= "Do not include links in this contact form. Use the format &quot;www.domain.com&quot; only.<br />";
                  break;
                  
                  }
            }
            
      if($onpageErrors == '') {
            //continue with form processing as usual
      
      }

}//end if form is submitted
?>

0
 
LVL 19

Expert Comment

by:v2Media
ID: 18882610
This is a snippet from a larger anti-spam script I use regularly. the line " $no_links = false;" can be deleted. Echo $onpageErrors someplace near your form if it doesn't = ''

e.g.

<?php
if($onpageErrors != '') {
?>
<div><?php echo $onpageErrors; ?></div>
<?php } ?>
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now