- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2016 09:00 PM
Hello there,
I am trying to build some requirement here which goes like:-
i want to validate a particular field with minimum 6 characters without spaces.
I mean to say is if the user types "abcdef"(6 characters without space) or "a a a a a a"(6 characters with space) etc., the script should return true else it should return false.
i have already tried that with a script but its not working for me:-
function onSubmit() {
//Type appropriate comment here, and begin script below
var check = g_form.getValue('my variable field name here');
var ok= /^\s*(\S\s*){6,}$/;
if(check.test(ok))
{
alert('Please Enter an appropriate name!');
return false;
}
else{
return true;
}
}
So any ideas on this?
Thanks,
Shahid
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2016 04:53 AM
return true condition you have written in two different lines. Thats why it is returning true if any of those conditions met.
Please use your script like below:
function onSubmit() {
var character1 = g_form.getValue('field1');
var character2 = g_form.getValue('field2');
var str1 = character1.replace(/ /g,"");
var str2 = character2.replace(/ /g,"");
if(str1.length >= 6 && str2.length >= 6 ){
return true;
}
else{
if(str1.length < 6){
alert('Please be appropriate in filling out the field "Field1"');
}
if(str2.length < 6){
alert('Please be appropriate in filling out the field "Field2"');
}
if(str1.length < 6 && str2.length < 6){
alert('Please be appropriate in filling out the field "Field1" and "Field2 "');
}
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2016 10:38 PM
Hi Shahid,
Try this thread:
http://stackoverflow.com/questions/1731190/javascript-check-if-a-string-has-white-space
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2016 01:52 AM
Hey Kushagra,
I have already tried the link you have posted above and thats the format which i am following as of now, which doesnt help.
Do you have any other way to do it?
Thanks,
Shahid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2016 10:57 PM
Hi Shahid,
Try this script:
function onSubmit() {
//Type appropriate comment here, and begin script below
var character = g_form.getValue('your variable field name here').toString();
var str = character.replace(" ","");
if(str.length >= 6){
return true;
}
else
return false;
}
Thanks,
Mihir Mohanta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2016 01:51 AM
Hi Mihir,
Thanks for replying.
The code given by you works gud but it does not work for the case "a a a a ".
The reason behind it that it just replaces the space between the first two characters and hence the form gets submitted.
The thing i want here is that the user wont be able to submit until and unless he has 6 characters(excluding space) in that field.
Hope you get it.
Thanks,
Shahid