Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Use indexOf to check field for certain characters.

arobertson
Tera Guru

Hi All,

I have the following onSubmit() script that checks to see if a field contains :// and if so, stops the form from being saved and displays a pop-up.

function onSubmit() {

  var name = g_form.getValue('name');

  var altname = g_form.getValue('u_alternative_name');

  if (name.indexOf('://') > -1 || altname.indexOf('://') > -1) {

  alert('Please enter only the domain without any prefix');

  g_form.submitted = false;

  return false;

  }

}

I have now been asked to amend the script to check if a , (comma) has been entered?

Any ideas on how to do this? Should i create an array of the letters, symbols that i want to search for?

1 ACCEPTED SOLUTION

bernyalvarado
Mega Sage

Hi Alex, since you're only checking for only two type of characters, I will perhaps keep it simple. If it grows above 4 type of characters, then you probably want to think in a different approach.



  if (name.indexOf('://') > -1 || altname.indexOf('://') > -1 || name.indexOf(',') > -1 || altname.indexOf(',') > -1) {  


            alert('Please enter only the domain without any prefix');  


            g_form.submitted = false;  


            return false;  


  }



Thanks,


Berny


View solution in original post

2 REPLIES 2

bernyalvarado
Mega Sage

Hi Alex, since you're only checking for only two type of characters, I will perhaps keep it simple. If it grows above 4 type of characters, then you probably want to think in a different approach.



  if (name.indexOf('://') > -1 || altname.indexOf('://') > -1 || name.indexOf(',') > -1 || altname.indexOf(',') > -1) {  


            alert('Please enter only the domain without any prefix');  


            g_form.submitted = false;  


            return false;  


  }



Thanks,


Berny


Works perfectly.