LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2015, 11:43 PM   #1
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Rep: Reputation: 30
how to JavaScript check validate param LENGTH?


If I have a HTML form that can input file, browse file
how to validate the file name LENGTH?

Code:
<script type="text/javascript">
function validate() {
var x = document.forms["myForm"]["photo"].value ;
var y = x.length ;
var extension = x.substr(y-3);
alert("begining... "+extension );

if ( x.match(/\./g) == null ) { alert("NO dot. type");  return false; }
if ( x.match(/\./g).length > 1 ) { alert("error");  return false; }

if ( x.match(/!/g)  != null ) { alert("symbol ! ");  return false; }
if ( x.match(/\@/g) != null ) { alert("symbol @ ");  return false; }
if ( x.match(/\#/g) != null ) { alert("symbol # ");  return false; }
if ( x.match(/\$/g) != null ) { alert("symbol $ ");  return false; }
if ( x.match(/\/g) != null ) { alert("symbol  ");  return false; }
if ( x.match(/\^/g) != null ) { alert("symbol ^ ");  return false; }
if ( x.match(/\&/g) != null ) { alert("symbol & ");  return false; }
if ( x.match(/\*/g) != null ) { alert("symbol * ");  return false; }
if ( x.match(/\(/g) != null ) { alert("symbol ( ");  return false; }
if ( x.match(/\)/g) != null ) { alert("symbol ) ");  return false; }
if ( x.match(/-/g) != null ) { alert("symbol - ");  return false; }

if ( y > 24 ) { alert("name too long " + y ); return false; }

if ( extension == "jpg" ) { alert("check 2 "+extension);
if ( x.match(/\./g).length == 1 ) { alert("symbol . ");  return true; }}
if ( extension == "png" ) { alert("check 2 "); 
if ( x.match(/\./g).length == 1 ) { alert("symbol . ");  return true; }}
if ( extension == "JPG" ) { alert("check 2 ");  return true; }
if ( extension == "PNG" ) { alert("check 2 ");  return true; }
if ( extension == "peg")  { alert("check 2 ");  return true; }

alert("finally...");
return false;
}
</script>
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files
<form name="myForm" onsubmit="return validate();" action="upload.cgi" enctype="multipart/form-data" method="post">
<p><input name="photo" type="file" style="height:100px"/></p>
<p><input name="Submit" type="submit" value="Submit UPLOADING" style="width:400px; height:200px"/></p>&nbsp;</form>
I got onsubmit JavaScript validate() function
I also got SCRIPT.PHP.CGI.ASP at server side

COPY AND PASTE INTO YOUR .HTML file see if work =)


filename-length means filename like
filename.jpg = 12 char include extension
filename12.jpg = 14 char include extension

Last edited by fhleung; 06-08-2015 at 08:46 PM.
 
Old 06-03-2015, 05:17 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,861
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you mean by validating the filename-length ?
 
Old 06-03-2015, 06:24 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
I suppose it would be something like this.name(length)

OK
 
Old 06-03-2015, 07:26 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,861
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
or this.name.length
 
Old 06-03-2015, 09:19 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I assume that part of what you want to do is block the file upload if the filename isn't valid? Here are some ideas on how to do that.

http://stackoverflow.com/q/4227043/240515

Last edited by dugan; 06-03-2015 at 11:57 AM.
 
Old 06-04-2015, 10:44 PM   #6
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
OK ! so like following?

Code:
var x = document.forms["myForm"]["photo"].value ;
var y = x.length ;
var filter =  /^[a-zA-Z0-9]+\.(jpg|png|jpeg|PNG)$/  ;
I re-do the code line by line checking, the code work or not depends on old new IE, FF

Last edited by fhleung; 06-08-2015 at 08:45 PM.
 
Old 06-09-2015, 09:11 PM   #7
fhleung
Member
 
Registered: Aug 2004
Distribution: Lubuntu Live OS
Posts: 432

Original Poster
Rep: Reputation: 30
also, anyone know how to check validate file SIZE?

ANSWER: var z = document.getElementById("fs").files[0].size ;

Code:
var z = document.getElementById("fs").files[0].size   ;   
<input name="photo" id="fs" type="file" style="height:100px"/>
http://www.w3schools.com/jsref/tryit...leupload_files

Last edited by fhleung; 06-23-2015 at 08:48 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Convert length-indicated variable length record file to LF-terminated Z038 Linux - General 10 11-29-2012 11:59 PM
Bash script to check the input string length fjkum Programming 3 06-30-2007 08:43 PM
[javascript] Check existance of value dlublink Programming 2 06-01-2006 06:07 AM
[BASH] How to filter characters away? !{param#word} {param%word}! Dark Carnival Programming 8 03-17-2005 01:49 PM
Javascript spell check slightcrazed Programming 3 10-02-2003 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration