Validate field with space and character limit!

shahid1
Kilo Expert

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

1 ACCEPTED SOLUTION

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;


}


}


View solution in original post

11 REPLIES 11

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


Mihir Mohanta
Kilo Sage

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


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