hello every1, i need help with the following questions:
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?
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?
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)
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?
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;
}
}
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
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).
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
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;
}
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?
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) {