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

Hi Shahid,



Please use


var str = character.replace(/ /g,"");



instead of


var str = character.replace(" ","");




Thanks,


Mihir


Hey Mihir,



It works great.



But i have one more problem here.



In case you want to put the validation for more than one field in the form , the script doesn't execute properly.



But for one field it works appropriately.



So, any go here?



Thanks,


Shahid


Hi Shahid,



I think the script I have provided should work for multiple field validation.



Can you share the script you are used ?



Thanks,


Mihir


Here you go!




function onSubmit() {


    //Type appropriate comment here, and begin script below  


  var character1 = g_form.getValue('field1');                                                                   //Please NOTE:-the validation for the field works without the use of toString() function.


var str1 = character1.replace(/ /g,"");


if(str1.length >= 6){


  return true;


}


else


  {


  alert('Please be appropriate in filling out the field "Field1"');


  return false;


}



  var character2 = g_form.getValue('field2');


              var str2 = character2.replace(/ /g,"");


    if(str2.length >= 6){


    return true;


    }


    else


    {


  alert('Please be appropriate in filling out the field "Field2"');


    return false;


    }


}



Have tried putting the toString for both the fields and for one fields at a time too but it doesnt help.



Any shoot?



Thanks,


Shahid


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;


}


}