Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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.
    Last edited by Philip M; 01-26-2013 at 07:21 PM.

    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.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    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";

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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;
    }

  • #5
    Regular Coder
    Join Date
    May 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks very much for this - worked a treat!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •