| | |
Trim Strings
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
as an evil one-liner:
or more useably:
if the String.match(/regex/) function returns a valid match for \w (any alphabetic character) or \d (any number) (notation [\w\d]) then theres nothing valid in the field.. change the regex to change the matching cases.
Some reading may be helpful:
http://www.webreference.com/js/column5/
(^ this ones a bit lame to be honest, check out the PERL regex tutorials, the syntax once your in an expression is identical: http://www.perl.com/pub/a/2000/11/begperl3.html)
if you want to trim the string... you can do a String.split(/[ ]{1,}/) (returns a String array); but if you don't know what's been entered you may need alot of logic to work out where your (valid) values are... if you split by an indefinate number of spaces ([ ]{1,}) then if the string is all spaces; the length of the resulting array (should) be 0...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<input onBlur="alert(this.value.match(/[\w\d]/)!=null?'OK':'NOT OK');">
or more useably:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function check_field(ident){ var target = document.getElementById(ident); return target.value.match(/[\w\d]/) != null; } function check_form(){ if(check_field('the_one_that_can't_be_spaces')){ //Everything's fine! }else{ //Something's missing o_O } }
if the String.match(/regex/) function returns a valid match for \w (any alphabetic character) or \d (any number) (notation [\w\d]) then theres nothing valid in the field.. change the regex to change the matching cases.
Some reading may be helpful:
http://www.webreference.com/js/column5/
(^ this ones a bit lame to be honest, check out the PERL regex tutorials, the syntax once your in an expression is identical: http://www.perl.com/pub/a/2000/11/begperl3.html)
if you want to trim the string... you can do a String.split(/[ ]{1,}/) (returns a String array); but if you don't know what's been entered you may need alot of logic to work out where your (valid) values are... if you split by an indefinate number of spaces ([ ]{1,}) then if the string is all spaces; the length of the resulting array (should) be 0...
Last edited by MattEvans; Nov 6th, 2006 at 2:52 pm.
Plato forgot the nullahedron..
Javascript is needed if you want your code to be validated before form submission. If you are going to validate after submit, you have to check your server-side language manual for the function that can check this for you. For example if you're using PHP, you can do something like this
$sumbitted_value=$_POST['field_name_that_should_not_be_blank'];
if (empty($submitted_value) or trim($submitted_value)==NULL)
{
echo 'Something's missing';
}
In this way we both check if the value is not empty, or when removed spaces from both ends the value is not null.
If you want to check if the value is numeric or char, it is completely different story.
What is not good with my method is that your users will have to go back to the previous page to change or enter the missing values.
Check the validation scripts that Dreamweaver offers, if you're familiar with Dreamweaver. It can check both - whether values exist and if the type of values correspond to the requirements. If error occures, an alert box will be showed.
$sumbitted_value=$_POST['field_name_that_should_not_be_blank'];
if (empty($submitted_value) or trim($submitted_value)==NULL)
{
echo 'Something's missing';
}
In this way we both check if the value is not empty, or when removed spaces from both ends the value is not null.
If you want to check if the value is numeric or char, it is completely different story.
What is not good with my method is that your users will have to go back to the previous page to change or enter the missing values.
Check the validation scripts that Dreamweaver offers, if you're familiar with Dreamweaver. It can check both - whether values exist and if the type of values correspond to the requirements. If error occures, an alert box will be showed.
Thanks to both of you. MattEvan's solution should be what I am looking for (validating from the client side). I am using ASP for the server side coding, where such validations are a breeze :-).
I am rather new to Javascript, have been working mostly in VB. Coming from such a VB background, I find it amazing that doing something simple like trimming a string takes so much effort, inspite of there being a inbuilt String class. Well.... I guess I'll just have to get used to it.
I am rather new to Javascript, have been working mostly in VB. Coming from such a VB background, I find it amazing that doing something simple like trimming a string takes so much effort, inspite of there being a inbuilt String class. Well.... I guess I'll just have to get used to it.
![]() |
Similar Threads
- Need help with Caesar Cipher program (Java)
- Error comparing strings from arrays (PHP)
- Compare strings... (C++)
- comparing two strings with linear search.. (Java)
- JSP and Oracle (JSP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: loading bar
- Next Thread: Connect submit button to a table
Views: 3592 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf






