Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2013, 06:31 PM   PM User | #1
howard-moore
Regular Coder

 
Join Date: May 2008
Posts: 114
Thanks: 13
Thanked 0 Times in 0 Posts
howard-moore is an unknown quantity at this point
Javascript form warning if field contains &

Hi,

I am trying to code a bit of JavaScript where it returns a warning if a field contains an ampersand: '&'

So far I have this:

Code:
<script language="JavaScript" type="text/javascript">
 <!--
function checkform ( form )
 {
   if (form.upload.value == "&") {
     alert( "Change the name of the file to remove the '&'." );
     form.upload.focus();
     return false ;
   }
   return true ;
 }
 //-->
 </script>
However, the problem I have is that this is asking for the whole filename to be '&', and not just appearing in the name. Can anyone suggest any changes to the script to help me?

Thanks,
Neil
howard-moore is offline   Reply With Quote
Old 01-26-2013, 07:10 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
if (/&/.test(form.upload.value)) {
alert( "Change the name of the file to remove the '&'." );
or

Code:
if (form.upload.value.indexOf("&") != -1) {
alert( "Change the name of the file to remove the '&'." );
You could of course simply erase any & or other illegal characters automatically.

Be aware that <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9). The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.



The seventh commandment is thou shalt not admit adultery.
- Pupil's answer to Catholic Elementary School test.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-26-2013 at 07:21 PM..
Philip M is offline   Reply With Quote
Old 01-27-2013, 02:11 AM   PM User | #3
howard-moore
Regular Coder

 
Join Date: May 2008
Posts: 114
Thanks: 13
Thanked 0 Times in 0 Posts
howard-moore is an unknown quantity at this point
Thanks for the reply, but it still appears to not be working. Below is the full script as shown on the page (and apologies for old code-style - it is coming from a pretty old app!):

Code:
<FORM id=adminform enctype="multipart/form-data" action="action=upload&dir=Public&order=name&srt=yes" method="post" name="form" onsubmit="return checkform(this);">
<INPUT type="hidden" name="MAX_FILE_SIZE" value="41943040"><INPUT type="hidden" name="confirm" value="true"><TABLE>
<TR><TD nowrap align="center"><INPUT class=button3 name="userfile[]" type="file" size="40"></TD></TR>
<TR><TD nowrap align="center"><INPUT class=button3 name="userfile[]" type="file" size="40"></TD></TR>
<TR><TD nowrap align="center"><INPUT class=button3 name="userfile[]" type="file" size="40"></TD></TR>
<TR><TD nowrap align="center"><INPUT class=button3 name="userfile[]" type="file" size="40"></TD></TR>
<TR><TD nowrap align="center"><INPUT class=button3 name="userfile[]" type="file" size="40"></TD></TR>
</TABLE>
<BR><TABLE><TR><TD><INPUT class=button type="submit" value="+  Upload"></TD>
<TD><input class=button type="button" value="x  Cancel" onClick="javascript:location='http://www.example.parishcouncil.net/admin/admin_docs/index.php?action=list&dir=Public&order=name&srt=yes';">
</TD></TR></FORM><script language="JavaScript" type="text/javascript">
 <!--
function checkform ( form )
 {
if (/&/.test(form.userfile.value)) {
alert( \"Change the name of the file to remove the '&'.\" );
     form.userfile.focus();
     return false ;
   }
   return true ;
 }
 //-->
 </script></TABLE><BR>\n";
howard-moore is offline   Reply With Quote
Old 01-27-2013, 04:46 AM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
First, use the error console and at least you'll see the syntax errors that prevent your code starting.

You are trying to test the group of fields as a single entity, which can't be done.

Just use this function:

Code:
function checkform( form )
{
  var inputs = form[ 'userfile[]' ],
      field,
      error = false;
 
  for( var i = 0; !error && ( field = inputs[ i ] ); i++ )
  {    
    if ( /&/.test( field.value ) ) 
    {
      error = true;
      alert( "Change the name of the file to remove the '&'." );
      setTimeout( function(){ field.focus() }, 10 );    
    }
  }

  return !error;
}
Logic Ali is offline   Reply With Quote
Old 01-27-2013, 04:19 PM   PM User | #5
howard-moore
Regular Coder

 
Join Date: May 2008
Posts: 114
Thanks: 13
Thanked 0 Times in 0 Posts
howard-moore is an unknown quantity at this point
Thanks very much for this - worked a treat!
howard-moore is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:06 PM.


Advertisement
Log in to turn off these ads.