SitePoint Sponsor |
|
User Tag List
Results 1 to 11 of 11
Thread: advace validation with js
-
Jan 8, 2008, 01:48 #1
advace validation with js
hello friends.
i am php programmer, i want to do js validation with my form.
in details like
!)i have product id, and that wanted to be an alphanumeric.
that is working perfectly fine with js.
2)but i want some formated alphanumeric value
e.g.
prod_id=L28
means first character should be alphabet and after i want only two digit
may be its too simple but i am new comer.
so please help me.
thanks in advance
-
Jan 8, 2008, 01:54 #2
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Easiest way to validate this is to use regular expressions. Get the value of the field and use the regular expression to validate.
-
Jan 8, 2008, 01:58 #3
could u please explain me with example means how i can do tht?
-
Jan 8, 2008, 02:30 #4
i ll tell you, what i am doing exactly,
PHP Code:if(document.productFrm.prod_id.value=="")
{
alert("Please enter your product id");
document.productFrm.prod_id.focus();
return false;
}
PHP Code:}else if (!ctype_alnum($prod_id)){
// echo " This is alphanumeric <br/>";
$prod_noErr = "Please enter alphanumeric value";
$validate = 0;
-
Jan 8, 2008, 02:36 #5
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Check out this thread.
You need to use the same concept. Your regular expressions to validate a word character followed by two numbers would look like this:
Code:var re = /^\w\d{2}$/;
Code:var value = document.productFrm.prod_id.value; var re = /^\w\d{2}$/; if (re.test(value)) { alert("Value is valid"); return true; } else { alert("Entered value is not correct"); return false; }
-
Jan 8, 2008, 02:43 #6
thanks a lot pepejeria,
i will have look first on your links, and then ll try to understand how it works, and then let you know ok.
-
Jan 8, 2008, 02:47 #7
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP also provides good Regular Expression support. You might also want to validate the value on the server as well.
-
Jan 8, 2008, 02:53 #8
yes i am working on that also
see
what i have done herePHP Code:function validate() {
var value = document.productFrm.prod_id.value;
var re = /^\w\d{2}$/;
if(document.productFrm.prod_id.value=="")
{
alert("Please enter your product id");
document.productFrm.prod_id.focus();
return false;
}
else if((document.productFrm.prod_id.value !="") && (isNaN(document.prod_id.value)==false))
{
if (re.test(value)) {
alert("Value is valid");
return true;
} else {
alert("Entered value is not correct");
return false;
}
}
}
query-
1)re.test(value)- what is this? (is this function)
-
Jan 8, 2008, 03:03 #9
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your code has several serious errors. Check out the Error Console in Firefox.
For example, missing } at the end and isNaN is misspelled. Also, why did you remove the backslashes from the regular expression?
-
Jan 8, 2008, 03:14 #10
ok, i have removed all errors
and pattern is
var re = /^\w\d{2}$/;
ok, but its not working. please tell what is going wrong here
-
Jan 8, 2008, 03:25 #11
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your if else logic is a bit odd:
Code:if (document.productFrm.prod_id.value=="") { } else if(document.productFrm.prod_id.value !="")
Anyway, I assume you read the other thread?
The ^ and the $ in the regular expression makes it unnecessary to check if the value empty is empty. Since it checks that the value starts with a word and ends with 2 numbers.
Bookmarks