1

I am new to ruby on rails. I have passed parameters ISDCode,AreaCode and Telephone number using POST from a form.

I have a string with information of the format countryName(ISDCode) passed in the variable ISDCode. For example "United States of America(+1)".

Now I want to save only the value of the ISDCode in the database.

What would be the ideal way to extract the ISD Code from the string?

Should I extract the ISD Code in Javascript before user POSTs the form or should I extract it in the model using a callback ?

Also is regex the only way to extract the information?

1
  • I have used CountryName(ISDCode) format for the ease of using auto complete in one text field. Commented Feb 26, 2012 at 11:35

5 Answers 5

1

Since the string is from auto completion, the ISDcodes should be existing in your database. So the best solution may be including an extra parameter (with a hidden input), like isdcode_id, then you simply use isdcode_id in your model. This way you can avoid the trouble to parse the string.

If this is not feasible, regex could be the best way to extract the information. You can override the setter in the model to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

use regular expression to match ISDcode

"United States of America(+1)" =~ /(\+[\d]+)/
puts $1

Comments

1

If you are interested in getting just the ISD Code alone, this should work:

"United States of America(+1)".gsub!(/[^\+\d]/, "")

NB: You can have this in your view helper and just call the helper on the string before persistence

Comments

1

Already answered, but I'd like to offer an alternative to getting the ISD Code:

    isd = "United States(+1)"
    puts isd[/[+]*[\d]{1,4}/] # +1

This regexp matches: 0001 +1 +01 etc.

Comments

0

I prefer to use js to extract information in the client side and make a validation in the model. By this way, you can get what you want and make sure it's correct.

2 Comments

I generally avoid validations in the client side as the client can easily cheat by turning javascript off on his browser but thanks anyways
By saying "make a validation in the model", I mean in the server side!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.