Web Analytics Made Easy -
StatCounter Embedded spaces in image name - CodingForum

Announcement

Collapse
No announcement yet.

Embedded spaces in image name

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Embedded spaces in image name

    I have a form that allows me to upload images to my site. I works fine. The upload is done using a Perl script. Now I am adding a validation script that checks a few things, one of which I do not know how to do.

    How do I determine if there are any embedded blanks in the image names? For example "my image name.jpg" would not be valid, but "my_image_name.jpg" would be OK.

    Thanks,
    -tdavis

  • #2
    This is what I have so far:

    var text = document.upload.elements.photo1.value;
    alert(text);
    for(c=0;c<text.length;c++) {
    if(text.charAt(c)==' ') {
    var errMsg = "Image names cannot have embedded blanks!";
    alert(errMsg);
    return;
    }}

    Not the best way I guess, but one problem is that I do not want to invalidate the file name if the path contains embedded blanks. That is OK. Only if the file name itself has embedded blanks. So I will have to figure out how to find the last slash I guess?

    Comment


    • #3
      Using split to get last piece

      Let me ask this another way. Is there a better way?
      And also, how can I put this into a function so that I can call it twenty times?

      var text = document.upload.elements.photo1.value;
      var spit = text.split("\\");
      var x;
      var count;
      for (x in spit)
      {
      count++;
      }
      text = spit[x];
      for(c=0;c<text.length;c++) {
      if(text.charAt(c)==' ') {
      var errMsg = "Image names cannot have embedded blanks!";
      frm.elements.photo1.className = "inputreq";
      alert(errMsg);
      return;
      }}

      Comment


      • #4
        Use a regular expression.

        PHP Code:
        var errMsg "";
        var 
        errStr "Image names can't contain spaces, line returns, tabs or null charactors!";
        function 
        takeNoSpaces(elmArraytypdef){
            for (var 
        i=elmArray.length-1;i>-1;i--){
        //  The editor is rejecting /\s/ for some reason.  the next line look like this:
        //      if ((elmArray[i].type == typ)&&(elmArray[i].value.match(/\s/) != null)){ 
                
        if ((elmArray[i].type == typ)&&(elmArray[i].value.match(/\s/) != null)){ 
                    
        errMsg errStr;
                    
        elmArray[i].value def;
                }
            }
        }
        //Pass in the entire elements array, validate elements of type of file, if invalid, default to "input required".
        takeNoSpaces(document.upload.elements"file""input required");
        // The above method can validate any number of input fields in your form.
        //  Change "file" to "text" to validate text fields
        if (errMsg != '')alert(errMsg); 
        Last edited by VortexCortex; Oct 5, 2006, 09:34 PM. Reason: vBulletin garbled my regular expression

        Comment


        • #5
          A working example.

          O I C. Just the last part of the file name must be devoid of spaces.

          My previous post keeps spaces out of any part of the file. Here's an example with an improved regular expression that lets the file path have spaces, just not the final file name...
          Code:
          <html>
          	<head>
          		<title>No Space File Validation</title>
          		<script type="text/javascript"><!--
          var errMsg = "";
          var errStr = "Image names can't contain spaces, line returns, tabs or null characters!";
          function takeNoSpaces(elmArray, typ, def){
          	for (var i=elmArray.length-1;i>-1;i--){
          		if (elmArray[i].type == typ) {
          			if (elmArray[i].value.match(/(^|[\\])[^\\]+\s[^\\]+$/) != null){ 
          				errMsg = errStr;
          				if (typ != "file") elmArray[i].value = def; //cant change file upload contents with JS.
          			}
          		}
          	}
          }
          
          function validate(){
          	//Pass in the entire elements array, validate elements of type of file, if invalid, default to "input required".
          	takeNoSpaces(document.upload.elements, "file", "input required");
          	// The above method can validate any number of input fields in your form.
          	//  Change "file" to "text" to validate text fields
          	if (errMsg != ''){
          		alert(errMsg);
          		errMsg = '';
          		return false;
          	}
          	return true;
          }
          //--></script>
          	</head>
          	<body>
          		<form name="upload" method="post" onsubmit="return validate()" enctype="multipart/form-data">
          			<input type="file" name="image1" size="30">
          			<input type="file" name="image2" size="30">
          			<input type="submit" value="upload">
          		</form>
          	</body>
          </html>

          Comment


          • #6
            This works great when I tested it as a standalone, but I cant get it to work as part of my external script. I have other validations that I am doing. I have 20 input fields, and I think it is validating those that are empty.

            Comment

            Working...
            X