A few questions regarding javascript

hello every1, i need help with the following questions:

  1. I have an upload form, before the image is being upload i want to check if that image really is an image, how to do it?

  2. i got a foldier containing images for my gallery page, while going through each file in that foldier i want also to check if that file is an image, how to do that aswell?

  3. i got a form that can update a certain date in 1 of my pages, how can i check if the uploaded text really is a date (ie 20/05/2010)

  4. i got a form that can upload text to DB, i dont want this text to contain more than 1 word, how do i check that this text isnt more than a word?

Thanks for all the help in advance

Thanks for the help :slight_smile:

regexp.test(string) and string.match(regexp) both do the same thing - it is just which of the regular expression and the string goes where that differs between them.

You mean .test() is actually implemented as follows?


function test(str) {
  return (this.match(str) == null);
}

It seems to me test() can be implemented more efficiently than match() because test() can stop and return false as soon as it finds one single character that does not match the pattern (lazy evaluation), whereas match() has to check the whole string to see if there is a part of that string that does match the pattern (greedy evaluation).

At least, that’s how I would implement it if I ever were to write a javascript VM (which I’m not) …

Well i’ve found an other way that seems to work fine…if soem1 else is intrested here it is…

    var anglitstr = ' '   <---- as u can see there's space between the ' ' symbols
          for (var i = 0; i < anglitstr.length; i++) {
              var anglitchar = anglitstr.charAt(i);
              if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                  alert("text cannot contain more than 1 word");
                  document.mdogs.desc1.focus();
                  return false;
              }
          }

You can’t - there is no way to examine the file until it reaches the server.

JavaScript can’t access files so how you do that depends on what server side language you use to process them on the server.

See http://javascript.about.com/library/bldate.htm

if fieldname.match(/^\S+$/) is true then fieldname doesn’t contain any whitespace (if you want to restrict it to letters and numbers instead of any non-whitespace use \W instead of \S

If it is returning null, then you could try the match method like so:

if (document.mdogs.desc1.value.match(/^\\S+$/) == null) {

Add a couple of diagnostic messages just before that code (if you don’t use a browser with built-in development tools or Firefox with the Firebug extension).

alert(document.mdogs.desc1.value);
alert(document.mdogs.desc1.value.match(/^\\S+$/));

I never use that way of accessing elements myself, so I probably can’t be of any assistance.

hmm, it still appears to skip that line even after the changes

          if (document.mdogs.desc1.value.match(/^\\S+$/) == false) {
              alert("this text cant contain more than 1 word");
              return false;
          }

all of my JS codes looks similar to this and they do work, so i cant really find out whats wrong :shifty:

Oops :x

You’re absolutely right, my bad …

See the ASP forum for background on this application. :wink:

You need to use the field value, not the field itself. Also, comparison for equality in JavaScript uses two equals signs. You’re trying to assign false to a function return value.

if (document.mdogs.desc1[b].value[/b].match(/^\\S+$/) [b]==[/b] false) {

I wasn’t commenting on the efficiency of either but simply pointing out that if you are going to use test() then you have to attach it to the regular expression and pass in the string. You can’t attach test() to a string because there is no such string method.

so

if (document.mdogs.desc1.value.test(/^\\S+$/) == false) {

will not work because document.mdogs.desc1.value isn’t a regular expression and /^\S+$/ isn’t a string. You’d need to code it as:

if (/^\\S+$/.test(document.mdogs.desc1.value) == false) {

Hey again all,
i got another question, until now i’ve been working with JS only with forms, now i got a Textarea in my page, i want to make a few checks on that textarea when the form is being submitted, and since its not related to a form, i dont know how to do that :slight_smile:

here’s the codes…

<textarea rows="4" cols="20" name="desc1">...</textarea>

JS code

 <script type="text/javascript" language="javascript">
      function checkfrm() {
          if (document.mdogs.desc1.value == "") {
              alert("&#1497;&#1513; &#1500;&#1502;&#1500;&#1488; &#1488;&#1514; &#1514;&#1497;&#1488;&#1493;&#1512; &#1495;&#1497;&#1497;&#1514; &#1492;&#1502;&#1495;&#1502;&#1491;");
              document.mdogs.desc1.focus();
              return false;
          }
          var anglitstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
          for (var i = 0; i < anglitstr.length; i++) {
              var anglitchar = anglitstr.charAt(i);
              if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                  alert("&#1497;&#1513; &#1500;&#1499;&#1514;&#1493;&#1489; &#1514;&#1497;&#1488;&#1493;&#1512; &#1495;&#1497;&#1497;&#1514; &#1492;&#1502;&#1495;&#1502;&#1491; &#1489;&#1506;&#1489;&#1512;&#1497;&#1514;");
                  document.mdogs.desc1.focus();
                  return false;
              }
          }
          var sofitstr = '&#1499;&#1510;&#1502;&#1508;&#1504;'
          for (var i = 0; i < sofitstr.length; i++) {
              var sofitchar = sofitstr.charAt(i);
              if (document.mdogs.desc1.value.charAt(document.mdogs.desc1.value.length - 1) == sofitchar) {
                  alert("&#1514;&#1497;&#1488;&#1493;&#1512; &#1495;&#1497;&#1497;&#1514; &#1492;&#1502;&#1495;&#1502;&#1491; &#1495;&#1497;&#1497;&#1489; &#1500;&#1492;&#1505;&#1514;&#1497;&#1497;&#1501; &#1506;&#1501; &#1488;&#1493;&#1514; &#1505;&#1493;&#1508;&#1497;&#1514;");
                  document.mdogs.desc1.focus();
                  return false;
              }
          }
          var anglitstr = '!@#$&#37;^&*)(_+|\\=-1234567890/][}{;:<,>?.'
          for (var i = 0; i < anglitstr.length; i++) {
              var anglitchar = anglitstr.charAt(i);
              if (document.mdogs.desc1.value.indexOf(anglitchar) != -1) {
                  alert("&#1488;&#1505;&#1493;&#1512; &#1513;&#1497;&#1492;&#1497;&#1493; &#1505;&#1497;&#1502;&#1504;&#1497;&#1501; &#1489;&#1514;&#1497;&#1488;&#1493;&#1512; &#1495;&#1497;&#1497;&#1514; &#1492;&#1502;&#1495;&#1502;&#1491;");
                  document.mdogs.desc1.focus();
                  return false;
              }
          }
          return true;
      }
  </script>

A textarea is a FORM ELEMENT, therefore it SHOULD be related to a form. (i.e. contained within the <form></form> tags). :wink:

You access it just the same as a text input form element. :slight_smile:

You might find this of interest. http://www5.brinkster.com/hiflyer/jscript/formchecktest.htm

if fieldname.match(/^\S+$/) is true then fieldname doesn’t contain any whitespace (if you want to restrict it to letters and numbers instead of any non-whitespace use \W instead of \S

hmm, this thing doesn’t work out for me, i’ve tried adding it to my javascript code but the code seems to just skip it without checking…did i wrote it right?

 if (document.mdogs.desc1.match(/^\\S+$/) = false) {
              alert("this text cant contain more than 1 word");
              return false;
 }

Thanks in advance

i treid it and tested, when i type “a” it alerts “a” and then again “a”
then i tried with a space like “a a”, first time it alerts “a a” and second time it alerts “null”…so i cant really get it, are there any other ways for checking if certain text doesn’t contain more than 1 word?

Thanks

Since you’re not interested in backreferences but merely want to know if the string matches the pattern you should proably use test() instead of match():

if (document.mdogs.desc1.value.[B]test[/B](/^\\S+$/) == false) {

:slight_smile:

Thanks, this one is working :slight_smile: