0

How I can add to these 2 scripts below a restriction, so it wont allow creating a hero with less than 3 characters in its name or when changing the name of the hero it wont allow less than 3 characters as well?

Currently everyone can create a hero with name: 1 or change their name .. basically all possible variations are available. I want to restrict it to minimum 3 characters name.

I'm using Microsoft SQL Server 2005 / 64 bit

Will be much appreciated.

Kind Regards.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NMALKO
  • 168
  • 1
  • 7

1 Answers1

1

At the start of your CREATE_NEW_CHAR procedure, you could do this...

IF LEN(LTRIM(@CharID)) < 3  -- LEN() does an RTRIM()
BEGIN
  SET @nRet = 7
  RETURN
END

The same in the CHANGE_NEW_ID procedure, but use IF LEN(LTRIM(@NewCharID)) < 3 instead.

The calling application is probably already checking the value of @nRet after executing the procedure. Hopefully it will give a generic error message if @nRet!=0. But, you may want to enhance that code, so that if @nRet=7 it can show a specific error message about the name entered being too short.

I've updated Pastebin also: CREATE_NEW_CHAR & CHANGE_NEW_ID

davmos
  • 9,324
  • 4
  • 40
  • 43
  • Can't I use these lines in my script that are already there or I must add the one you suggested ? ... `SET @x = 1` and `WHILE (@x <= LEN(@CharID))` ? – NMALKO May 12 '13 at 11:28
  • I think you want to check the length before entering the `WHILE` loop to validate the individual characters. – davmos May 12 '13 at 11:32
  • It doesnt matter where exactly it will do the check .. the only thing that matters is when they enter less than 3 characters and click the button to create the hero, so the script will restrict them and wont create it until they use 3 or more characters and not less than 3. So, finally could you please upload to pastebin the both edited scripts with the restriction, would be much appreciated since I dont want to mess up something .. because i cant make a db backup right now and to be safe. Once again will appreciate it a lot. – NMALKO May 12 '13 at 11:34
  • 1
    Yes, so if you put the above `IF` before the `WHILE` loop, it will exit the procedure immediately when the name is not long enough. If you want to tell the user the reason why it didn't work, check the value of `nRet`. – davmos May 12 '13 at 11:37
  • Should I remove the `WHILE (@x <= LEN(@CharID))` line from the current script ? – NMALKO May 12 '13 at 11:44
  • Nah, the change is just addition, no modification of existing code. Let me see if I can update pastebin. – davmos May 12 '13 at 11:45
  • Yeah, there is an option to create new version of the current pastebin posted. – NMALKO May 12 '13 at 11:47
  • 1
    OK cool, not used Pastebin before, but yeah it was easy ;-) links added to answer. – davmos May 12 '13 at 11:53
  • I've received an error probably because it was nRet without @ .. I've added it and it executed successfully, right ? Will test now if it works. – NMALKO May 12 '13 at 12:01
  • oops yes sorry. fixed pastebin and answer. – davmos May 12 '13 at 12:04
  • I think it works, and just one last thing how I will be able to change the nret message now ? Because 5 and 7 are for something different. Also, this check you have suggested only restric for characters under 3 characters in the name, not anything other, correctly ? – NMALKO May 12 '13 at 12:05
  • In that case .. last thing that I am curious why we should use LTRIM or it's necessary ? – NMALKO May 12 '13 at 12:14
  • If the parameter contains leading spaces, they will be included in the output of `LEN()`. If you want to allow leading spaces in the name (I doubt it?) or you are sure the calling app is trimming it, then you don't need it. – davmos May 12 '13 at 12:22
  • For checking @nRet, can you post the code that does that already? – davmos May 12 '13 at 12:25
  • Will do post, so to sum it up LTRIM will remove these leading spaces because I dont want to allow them for sure. – NMALKO May 12 '13 at 12:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29796/discussion-between-davmos-and-nmalko) – davmos May 12 '13 at 12:35